Skip to content

Commit

Permalink
Add rector + Raise PHP to 8.0 (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
xepozz and vjik authored Feb 11, 2023
1 parent 40fbb41 commit e26763a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1', '8.2']
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1', '8.2']
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.0.4 under development

- no changes in this release.
- Enh #41: Raise minimum PHP version to 8.0 (@vjik, @xepozz)

## 1.0.3 June 27, 2022

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ All you need is to use any [PSR-11](https://www.php-fig.org/psr/psr-11/) compati

## Requirements

- PHP 7.4 or higher.
- PHP 8.0 or higher.

## Installation

The package could be installed with composer:

```shell
composer require yiisoft/yii-event --prefer-dist
composer require yiisoft/yii-event
```

## General usage
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"source": "https://github.com/yiisoft/yii-event"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"psr/container": "^1.0|^2.0",
"yiisoft/event-dispatcher": "^1.0",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/injector": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.15.13",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.6",
Expand Down
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php80\Rector\Ternary\GetDebugTypeRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
GetDebugTypeRector::class,
]);
};
18 changes: 7 additions & 11 deletions src/CallableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
*/
final class CallableFactory
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
public function __construct(
private ContainerInterface $container
) {
}

/**
* Create real callable listener from definition.
*
* @param mixed $definition Defintion to create listener from.
* @param mixed $definition Definition to create listener from.
*
* @throws InvalidListenerConfigurationException Failed to create listener.
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
public function create($definition): callable
public function create(mixed $definition): callable
{
/** @var mixed */
$callable = $this->prepare($definition);
Expand All @@ -46,13 +44,11 @@ public function create($definition): callable
}

/**
* @param mixed $definition
*
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*
* @return mixed
*/
private function prepare($definition)
private function prepare(mixed $definition)
{
if (is_string($definition) && $this->container->has($definition)) {
return $this->container->get($definition);
Expand All @@ -79,7 +75,7 @@ private function prepare($definition)

try {
$reflection = new ReflectionMethod($className, $methodName);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
return null;
}
if ($reflection->isStatic()) {
Expand Down
25 changes: 9 additions & 16 deletions src/ListenerCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@
use Yiisoft\EventDispatcher\Provider\ListenerCollection;
use Yiisoft\Injector\Injector;

use function get_class;
use function is_object;
use function is_string;

final class ListenerCollectionFactory
{
private Injector $injector;
private CallableFactory $callableFactory;

public function __construct(Injector $injector, CallableFactory $callableFactory)
{
$this->injector = $injector;
$this->callableFactory = $callableFactory;
public function __construct(
private Injector $injector,
private CallableFactory $callableFactory,
) {
}

/**
Expand All @@ -37,7 +32,7 @@ public function create(array $eventListeners): ListenerCollection
}

if (!is_iterable($listeners)) {
$type = is_object($listeners) ? get_class($listeners) : gettype($listeners);
$type = get_debug_type($listeners);

throw new InvalidEventConfigurationFormatException(
"Event listeners for $eventName must be an iterable, $type given."
Expand All @@ -48,12 +43,10 @@ public function create(array $eventListeners): ListenerCollection
foreach ($listeners as $callable) {
$listener =
/** @return mixed */
function (object $event) use ($callable) {
return $this->injector->invoke(
$this->callableFactory->create($callable),
[$event]
);
};
fn (object $event) => $this->injector->invoke(
$this->callableFactory->create($callable),
[$event]
);
$listenerCollection = $listenerCollection->add($listener, $eventName);
}
}
Expand Down
34 changes: 11 additions & 23 deletions src/ListenerConfigurationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@

use Psr\Container\ContainerExceptionInterface;

use function get_class;
use function gettype;
use function is_array;
use function is_object;
use function is_string;

/**
* ListenerConfigurationChecker could be used in development mode to check if listeners are defined correctly.
* `ListenerConfigurationChecker` could be used in development mode to check if listeners are defined correctly.
*
* ```php
* $checker->check($configuration->get('events-web'));
* ```
*/
final class ListenerConfigurationChecker
{
private CallableFactory $callableFactory;

public function __construct(CallableFactory $callableFactory)
{
$this->callableFactory = $callableFactory;
public function __construct(
private CallableFactory $callableFactory,
) {
}

/**
Expand All @@ -48,7 +44,7 @@ public function check(array $configuration): void
}

if (!is_iterable($listeners)) {
$type = is_object($listeners) ? get_class($listeners) : gettype($listeners);
$type = get_debug_type($listeners);

throw new InvalidEventConfigurationFormatException(
"Event listeners for $eventName must be an iterable, $type given."
Expand All @@ -75,10 +71,7 @@ public function check(array $configuration): void
}
}

/**
* @param mixed $definition
*/
private function createNotCallableMessage($definition): string
private function createNotCallableMessage(mixed $definition): string
{
if (is_string($definition) && class_exists($definition)) {
if (!method_exists($definition, '__invoke')) {
Expand Down Expand Up @@ -109,7 +102,7 @@ private function createNotCallableMessage($definition): string
return sprintf(
'"%s" method is not defined in "%s" class.',
$definition[1],
get_class($definition[0]),
$definition[0]::class,
);
}
}
Expand All @@ -118,26 +111,21 @@ private function createNotCallableMessage($definition): string
}

/**
* @param mixed $definition
*
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
private function isCallable($definition): bool
private function isCallable(mixed $definition): bool
{
try {
$this->callableFactory->create($definition);
} catch (InvalidListenerConfigurationException $e) {
} catch (InvalidListenerConfigurationException) {
return false;
}

return true;
}

/**
* @param mixed $listener
*/
private function listenerDump($listener): string
private function listenerDump(mixed $listener): string
{
return is_object($listener) ? get_class($listener) : var_export($listener, true);
return is_object($listener) ? $listener::class : var_export($listener, true);
}
}

0 comments on commit e26763a

Please sign in to comment.