Skip to content

Commit

Permalink
Merge pull request #126 from PavelJurasek/php8
Browse files Browse the repository at this point in the history
PHP 8 support
  • Loading branch information
Spamercz authored Jun 11, 2021
2 parents c0ab8d2 + f4b0fe7 commit d3e8e69
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 73 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/php-package-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Package CI

on:
pull_request:

jobs:
checks:
name: Checks
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 7.4, 8.0 ]
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- run: make composer

- run: make phpstan

- run: make tester
47 changes: 0 additions & 47 deletions .travis.yml

This file was deleted.

13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
composer:
composer validate
composer update --no-interaction --prefer-dist

cs:
vendor/bin/phpcs --standard=./ruleset.xml --cache=${HOME}/phpcs-cache/.phpcs-cache --encoding=utf-8 -sp src tests/KdybyTests
vendor/bin/parallel-lint -e php,phpt --exclude vendor .

phpstan:
vendor/bin/phpstan analyse src tests/KdybyTests

tester:
vendor/bin/tester -s -c ./tests/php.ini-unix ./tests/KdybyTests/
16 changes: 4 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,25 @@
"email": "[email protected]"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/CZechBoY/Kdyby-CodingStandard.git"
}
],
"support": {
"email": "[email protected]",
"issues": "https://github.com/kdyby/events/issues"
},
"require": {
"php": "^7.1",
"php": "^7.4 | ^8.0",
"nette/di": "^3.0",
"nette/utils": "^3.0",
"nette/reflection": "^2.4"
"nette/utils": "^3.0"
},
"require-dev": {
"kdyby/coding-standard": "^1.0@dev",
"nette/application": "^3.0",
"nette/bootstrap": "^3.0",
"nette/caching": "^3.0",
"nette/security": "^3.0",
"nette/http": "^3.0",
"tracy/tracy": "^2.5",
"symfony/event-dispatcher": "^3.0 || ^4.0",
"nette/tester": "^2.2",
"phpstan/phpstan-shim": "^0.11",
"nette/tester": "^2.4",
"phpstan/phpstan": "^0.12.88",
"jakub-onderka/php-parallel-lint": "^1.0"
},
"autoload": {
Expand Down
7 changes: 6 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
level: 7
level: 5
paths:
- src
- tests/KdybyTests

ignoreErrors:
-
message: '~Method Kdyby\\Events\\SymfonyDispatcher::dispatch\(\) should return object but return statement is missing\.~'
path: src/Events/SymfonyDispatcher.php
23 changes: 14 additions & 9 deletions src/Events/DI/EventsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use Nette\PhpGenerator\ClassType as ClassTypeGenerator;
use Nette\PhpGenerator\Helpers as GeneratorHelpers;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Reflection\ClassType as ClassTypeReflection;
use Nette\Utils\Validators;
use ReflectionProperty;
use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
Expand Down Expand Up @@ -352,7 +351,6 @@ private function autowireEvents(DIContainerBuilder $builder)
continue;
}

/** @var \Nette\DI\Definitions\ServiceDefinition $def */
if ($this->isAlias($def)) {
continue; // alias
}
Expand Down Expand Up @@ -389,11 +387,11 @@ private function autowireEvents(DIContainerBuilder $builder)
continue;
}

$this->bindEventProperties($def, ClassTypeReflection::from($class));
$this->bindEventProperties($def, new \ReflectionClass($class));
}
}

protected function bindEventProperties(Definition $def, ClassTypeReflection $class)
protected function bindEventProperties(Definition $def, \ReflectionClass $class)
{
/** @var \Nette\DI\Definitions\ServiceDefinition $def */
$def = $def instanceof FactoryDefinition ? $def->getResultDefinition() : $def;
Expand All @@ -404,18 +402,17 @@ protected function bindEventProperties(Definition $def, ClassTypeReflection $cla
continue;
}

if ($property->hasAnnotation('persistent') || $property->hasAnnotation('inject')) { // definitely not an event
if (self::propertyHasAnnotation($property, 'persistent') || self::propertyHasAnnotation($property, 'inject')) { // definitely not an event
continue;
}

$dispatchAnnotation = self::propertyHasAnnotation($property, 'globalDispatchFirst');
$def->addSetup('$' . $name, [
new Statement($this->prefix('@manager') . '::createEvent', [
[$class->getName(), $name],
new PhpLiteral('$service->' . $name),
NULL,
$property->hasAnnotation('globalDispatchFirst')
? (bool) $property->getAnnotation('globalDispatchFirst')
: $this->loadedConfig['globalDispatchFirst'],
$dispatchAnnotation ?? $this->loadedConfig['globalDispatchFirst'],
]),
]);
}
Expand Down Expand Up @@ -489,12 +486,20 @@ private static function createEventSubscriberInstanceWithoutConstructor($class)
throw new \InvalidArgumentException('Given class cannot be NULL');
}

$instance = ClassTypeReflection::from($class)->newInstanceWithoutConstructor();
$instance = (new \ReflectionClass($class))->newInstanceWithoutConstructor();
if (!$instance instanceof EventSubscriber) {
throw new \Kdyby\Events\UnexpectedValueException(sprintf('The class %s does not implement %s', $class, EventSubscriber::class));
}

return $instance;
}

private static function propertyHasAnnotation(ReflectionProperty $property, string $annotation): ?bool
{
$comment = $property->getDocComment();

$exists = \strpos($comment, "@$annotation") !== FALSE;

return $exists ? !\stripos($comment, "@$annotation false") : NULL;
}
}
2 changes: 1 addition & 1 deletion src/Events/Diagnostics/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ protected function renderStyles()
public static function register(EventManager $eventManager, DIContainer $sl)
{
/** @var \Kdyby\Events\Diagnostics\Panel $panel */
$panel = new static($sl);
$panel = new Panel($sl);
$panel->setEventManager($eventManager);
Debugger::getBar()->addPanel($panel);

Expand Down
3 changes: 1 addition & 2 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use ArrayIterator;
use Doctrine\Common\EventArgs;
use Kdyby\Events\Diagnostics\Panel;
use Nette\Reflection\ClassType as ClassTypeReflection;
use Nette\Utils\Callback;
use Traversable;

Expand Down Expand Up @@ -168,7 +167,7 @@ public function getListeners()
$args = new EventArgsList(func_get_args());

} else {
$args = ClassTypeReflection::from($argsClass)->newInstanceArgs(func_get_args());
$args = (new \ReflectionClass($argsClass))->newInstanceArgs(func_get_args());
}

assert($args instanceof EventArgs);
Expand Down
5 changes: 4 additions & 1 deletion src/Events/SymfonyDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Kdyby\Events;

use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
use Symfony\Contracts\EventDispatcher\Event as SymfonyEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SymfonyDispatcher implements \Symfony\Component\EventDispatcher\EventDispatcherInterface
Expand All @@ -26,6 +26,9 @@ public function __construct(EventManager $eventManager)
$this->evm = $eventManager;
}

/**
* @param string|object $eventName
*/
public function dispatch($eventName, SymfonyEvent $event = NULL)
{
$this->evm->dispatchEvent($eventName, new EventArgsList([$event]));
Expand Down

0 comments on commit d3e8e69

Please sign in to comment.