diff --git a/.travis.yml b/.travis.yml index aa40ddc..4f047b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,9 @@ cache: - $HOME/.composer/cache php: - - 5.6 - - 7.0 - 7.1 - 7.2 + - 7.3 env: - # dev @@ -20,20 +19,18 @@ env: matrix: fast_finish: true include: - - php: 7.2 + - php: 7.3 env: COMPOSER_EXTRA_ARGS="--prefer-stable" COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg" - - php: 7.2 + - php: 7.3 env: COMPOSER_EXTRA_ARGS="--prefer-stable" PHPSTAN=1 - - php: 7.2 + - php: 7.3 env: COMPOSER_EXTRA_ARGS="--prefer-stable" CODING_STANDARD=1 allow_failures: - env: - - php: 7.2 + - php: 7.3 env: COMPOSER_EXTRA_ARGS="--prefer-stable" COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg" install: - - if [ "$CODING_STANDARD" = "1" ]; then composer require --dev --no-update kdyby/coding-standard:^1.0@dev; fi - - if [ "$PHPSTAN" = "1" ]; then composer require --dev --no-update phpstan/phpstan-shim:^0.7; fi - travis_retry composer update --no-interaction --no-suggest --no-progress --prefer-dist $COMPOSER_EXTRA_ARGS - travis_retry composer create-project --no-interaction jakub-onderka/php-parallel-lint /tmp/php-parallel-lint - if [ "$COVERAGE" != "" ]; then travis_retry wget -O /tmp/coveralls.phar https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar; fi @@ -41,7 +38,7 @@ install: script: - vendor/bin/tester $COVERAGE -s -p ${TESTER_RUNTIME:-php} -c ./tests/php.ini-unix ./tests/KdybyTests/ - php /tmp/php-parallel-lint/parallel-lint.php -e php,phpt --exclude vendor . - - if [ "$PHPSTAN" = "1" ]; then php vendor/phpstan/phpstan-shim/phpstan.phar analyse --ansi --no-progress -l7 -c phpstan.neon src tests/KdybyTests; fi + - if [ "$PHPSTAN" = "1" ]; then php vendor/bin/phpstan analyse --ansi --no-progress; fi - if [ "$CODING_STANDARD" = "1" ]; then php vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 -sp src tests; fi after_script: diff --git a/composer.json b/composer.json index 588c087..c36f2af 100644 --- a/composer.json +++ b/composer.json @@ -17,13 +17,14 @@ "issues": "https://github.com/kdyby/events/issues" }, "require": { - "php": "^5.6 || ^7.0", + "php": "^7.1", "nette/di": "^2.4.8@dev", "nette/utils": "^2.4.5@dev", "nette/reflection": "^2.4@dev", "kdyby/strict-objects": "^1.0" }, "require-dev": { + "kdyby/coding-standard": "^1.0@dev", "nette/application": "^2.4@dev", "nette/bootstrap": "^2.4@dev", "nette/caching": "^2.5@dev", @@ -33,7 +34,8 @@ "symfony/event-dispatcher": "^3.0 || ^4.0", - "nette/tester": "^2.0" + "nette/tester": "^2.0", + "phpstan/phpstan-shim": "^0.11" }, "minimum-stability": "dev", "autoload": { diff --git a/phpstan.neon b/phpstan.neon deleted file mode 100644 index cd000db..0000000 --- a/phpstan.neon +++ /dev/null @@ -1,3 +0,0 @@ -parameters: - ignoreErrors: - - '#Constant TEMP_DIR not found#' diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..ba9bc6b --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,5 @@ +parameters: + level: 7 + paths: + - src + - tests/KdybyTests diff --git a/src/Events/DI/EventsExtension.php b/src/Events/DI/EventsExtension.php index b470188..1cc989d 100644 --- a/src/Events/DI/EventsExtension.php +++ b/src/Events/DI/EventsExtension.php @@ -119,7 +119,7 @@ public function loadConfiguration() is_string($subscriber) ? new Statement($subscriber) : $subscriber, ])[0]); - list($subscriberClass) = (array) $builder->normalizeEntity($def->getEntity()); + [$subscriberClass] = (array) $builder->normalizeEntity($def->getEntity()); if (class_exists($subscriberClass)) { $def->setClass($subscriberClass); } @@ -174,9 +174,9 @@ public function afterCompile(ClassTypeGenerator $class) /** @hack This tries to add the event invokation right after the code, generated by NetteExtension. */ $foundNetteInitStart = $foundNetteInitEnd = FALSE; - $lines = explode(";\n", trim($init->getBody())); + $lines = explode(";\n", trim((string) $init->getBody())); $init->setBody(NULL); - while (($line = array_shift($lines)) || $lines) { + while (($line = array_shift($lines)) !== NULL) { if ($foundNetteInitStart && !$foundNetteInitEnd && stripos($line, 'Nette\\') === FALSE && stripos($line, 'set_include_path') === FALSE && stripos($line, 'date_default_timezone_set') === FALSE ) { @@ -237,7 +237,9 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit ); } - if (!$def->getClass()) { + $defClass = $def->getClass(); + + if (!$defClass) { throw new \Nette\Utils\AssertionException( sprintf( 'Please, specify existing class for %sservice @%s explicitly, and make sure, that the class exists and can be autoloaded.', @@ -246,30 +248,32 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit ) ); - } elseif (!class_exists($def->getClass())) { + } + + if (!class_exists($defClass)) { throw new \Nette\Utils\AssertionException( sprintf( 'Class %s of %sservice @%s cannot be found. Please make sure, that the class exists and can be autoloaded.', - $def->getClass(), + $defClass, is_numeric($serviceName) ? 'anonymous ' : '', $serviceName ) ); } - if (!in_array(EventSubscriber::class, class_implements($def->getClass()))) { + if (!in_array(EventSubscriber::class, class_implements($defClass), TRUE)) { // the minimum is Doctrine EventSubscriber, but recommend is Kdyby Subscriber throw new \Nette\Utils\AssertionException(sprintf('Subscriber @%s doesn\'t implement %s.', $serviceName, Subscriber::class)); } $eventNames = []; - $listenerInst = self::createEventSubscriberInstanceWithoutConstructor($def->getClass()); + $listenerInst = self::createEventSubscriberInstanceWithoutConstructor($defClass); foreach ($listenerInst->getSubscribedEvents() as $eventName => $params) { if (is_numeric($eventName) && is_string($params)) { // [EventName, ...] - list(, $method) = Event::parseName($params); + [, $method] = Event::parseName($params); $eventNames[] = ltrim($params, '\\'); if (!method_exists($listenerInst, $method)) { - throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $def->getClass(), $method)); + throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $defClass, $method)); } } elseif (is_string($eventName)) { // [EventName => ???, ...] @@ -277,18 +281,18 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit if (is_string($params)) { // [EventName => method, ...] if (!method_exists($listenerInst, $params)) { - throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $def->getClass(), $params)); + throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $defClass, $params)); } } elseif (is_string($params[0])) { // [EventName => [method, priority], ...] if (!method_exists($listenerInst, $params[0])) { - throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $def->getClass(), $params[0])); + throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $defClass, $params[0])); } } else { foreach ($params as $listener) { // [EventName => [[method, priority], ...], ...] if (!method_exists($listenerInst, $listener[0])) { - throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $def->getClass(), $listener[0])); + throw new \Nette\Utils\AssertionException(sprintf('Event listener %s::%s() is not implemented.', $defClass, $listener[0])); } } } @@ -327,8 +331,9 @@ private function autowireEvents(DIContainerBuilder $builder) $class = $builder->expand($def->getEntity()); if (is_array($class)) { continue; + } - } elseif (!class_exists($class)) { + if (!class_exists($class)) { continue; } } @@ -373,7 +378,7 @@ private function optimizeListeners(DIContainerBuilder $builder) $listeners = []; foreach ($this->listeners as $serviceName => $eventNames) { foreach ($eventNames as $eventName) { - list($namespace, $event) = Event::parseName($eventName); + [$namespace, $event] = Event::parseName($eventName); $listeners[$eventName][] = $serviceName; if (!$namespace || !class_exists($namespace)) { @@ -417,7 +422,7 @@ private function filterArgs($statement) */ public static function register(Configurator $configurator) { - $configurator->onCompile[] = function ($config, Compiler $compiler) { + $configurator->onCompile[] = static function ($config, Compiler $compiler) { $compiler->addExtension('events', new EventsExtension()); }; } diff --git a/src/Events/Diagnostics/Panel.php b/src/Events/Diagnostics/Panel.php index 16de42b..87e0d65 100644 --- a/src/Events/Diagnostics/Panel.php +++ b/src/Events/Diagnostics/Panel.php @@ -70,7 +70,7 @@ class Panel implements \Tracy\IBarPanel private $registeredClasses; /** - * @var bool + * @var bool|array */ public $renderPanel = TRUE; @@ -151,7 +151,7 @@ public function getTab() } return '' - . '' + . '' . '' . count(Arrays::flatten($this->dispatchLog)) . ' calls' . ''; } @@ -190,7 +190,7 @@ public function getPanel() $totalListeners = count(array_unique(Arrays::flatten($this->listenerIds))); return '' . - '

' . $h($totalEvents) . ' registered events, ' . $h($totalListeners) . ' registered listeners

' . + '

' . $h((string) $totalEvents) . ' registered events, ' . $h((string) $totalListeners) . ' registered listeners

' . '
' . $s . '
'; } @@ -350,7 +350,7 @@ private function renderListeners($ids) $h = 'htmlspecialchars'; - $shortFilename = function (ReflectionFunctionAbstract $refl) { + $shortFilename = static function (ReflectionFunctionAbstract $refl) { $title = '.../' . basename($refl->getFileName()) . ':' . $refl->getStartLine(); /** @var string|NULL $editor */ @@ -425,7 +425,7 @@ private function renderCalls(array $calls) { static $runIcon; if (empty($runIcon)) { - $runIcon = ''; + $runIcon = ''; } $s = ''; diff --git a/src/Events/Event.php b/src/Events/Event.php index a937fb1..5a714db 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -11,6 +11,7 @@ namespace Kdyby\Events; use ArrayIterator; +use Doctrine\Common\EventArgs; use Kdyby\Events\Diagnostics\Panel; use Nette\Reflection\ClassType as ClassTypeReflection; use Nette\Utils\Callback; @@ -46,7 +47,7 @@ class Event implements \ArrayAccess, \IteratorAggregate, \Countable private $namespace; /** - * @var \Kdyby\Events\EventManager + * @var \Kdyby\Events\EventManager|null */ private $eventManager; @@ -56,18 +57,18 @@ class Event implements \ArrayAccess, \IteratorAggregate, \Countable private $argsClass; /** - * @var \Kdyby\Events\Diagnostics\Panel + * @var \Kdyby\Events\Diagnostics\Panel|null */ private $panel; /** * @param string|array $name - * @param array $defaults + * @param array|\Traversable|null $defaults * @param string $argsClass */ public function __construct($name, $defaults = [], $argsClass = NULL) { - list($this->namespace, $this->name) = self::parseName($name); + [$this->namespace, $this->name] = self::parseName($name); $this->argsClass = $argsClass; if (is_array($defaults) || $defaults instanceof Traversable) { @@ -162,8 +163,9 @@ public function getListeners() $name = $this->getName(); $evm = $this->eventManager; + assert($evm !== NULL); $argsClass = $this->argsClass; - $globalDispatch = function () use ($name, $evm, $argsClass) { + $globalDispatch = static function () use ($name, $evm, $argsClass) { if ($argsClass === NULL) { $args = new EventArgsList(func_get_args()); @@ -171,6 +173,8 @@ public function getListeners() $args = ClassTypeReflection::from($argsClass)->newInstanceArgs(func_get_args()); } + assert($args instanceof EventArgs); + $evm->dispatchEvent($name, $args); }; @@ -193,7 +197,7 @@ public function __invoke() } /** - * @param string $name + * @param string|array $name * @return array */ public static function parseName(&$name) @@ -271,7 +275,7 @@ public function offsetSet($index, $item) public function offsetGet($index) { if (!$this->offsetExists($index)) { - throw new \Kdyby\Events\OutOfRangeException; + throw new \Kdyby\Events\OutOfRangeException(); } return $this->listeners[$index]; @@ -279,7 +283,6 @@ public function offsetGet($index) /** * @param int $index - * * @return bool */ public function offsetExists($index) diff --git a/src/Events/EventManager.php b/src/Events/EventManager.php index a53f603..aea5d72 100644 --- a/src/Events/EventManager.php +++ b/src/Events/EventManager.php @@ -26,31 +26,31 @@ class EventManager extends \Doctrine\Common\EventManager /** * [Event => [Priority => [[Listener, method], Subscriber, Subscriber, ...]]] * - * @var array[] + * @var array> */ private $listeners = []; /** * [Event => Subscriber|callable] * - * @var \Doctrine\Common\EventSubscriber[][]|callable[][] + * @var array<\Doctrine\Common\EventSubscriber[]|callable[]> */ private $sorted = []; /** * [SubscriberHash => Subscriber] * - * @var \Doctrine\Common\EventSubscriber[] + * @var array */ private $subscribers = []; /** - * @var \Kdyby\Events\Diagnostics\Panel + * @var \Kdyby\Events\Diagnostics\Panel|null */ private $panel; /** - * @var \Kdyby\Events\IExceptionHandler + * @var \Kdyby\Events\IExceptionHandler|null */ private $exceptionHandler; @@ -83,13 +83,15 @@ public function dispatchEvent($eventName, DoctrineEventArgs $eventArgs = NULL) $this->panel->eventDispatch($eventName, $eventArgs); } - list($namespace, $event) = Event::parseName($eventName); + [, $event] = Event::parseName($eventName); foreach ($this->getListeners($eventName) as $listener) { try { if ($listener instanceof EventSubscriber) { $listener = [$listener, $event]; } + assert(is_callable($listener)); + if ($eventArgs instanceof EventArgsList) { /** @var \Kdyby\Events\EventArgsList $eventArgs */ call_user_func_array($listener, $eventArgs->getArgs()); @@ -115,7 +117,7 @@ public function dispatchEvent($eventName, DoctrineEventArgs $eventArgs = NULL) /** * Gets the listeners of a specific event or all listeners. * - * @param string $eventName + * @param string|null $eventName * @return \Doctrine\Common\EventSubscriber[]|callable[]|\Doctrine\Common\EventSubscriber[][]|callable[][] */ public function getListeners($eventName = NULL) @@ -128,7 +130,7 @@ public function getListeners($eventName = NULL) return $this->sorted[$eventName]; } - foreach ($this->listeners as $event => $prioritized) { + foreach (array_keys($this->listeners) as $event) { if (!isset($this->sorted[$event])) { $this->sortListeners($event); } @@ -140,7 +142,7 @@ public function getListeners($eventName = NULL) /** * Checks whether an event has any registered listeners. * - * @param string $eventName + * @param string|null $eventName * @return bool TRUE if the specified event has any listeners, FALSE otherwise. */ public function hasListeners($eventName) @@ -154,19 +156,18 @@ public function hasListeners($eventName) * @param string|array $events The event(s) to listen on. * @param \Doctrine\Common\EventSubscriber|\Closure|array $subscriber The listener object. * @param int $priority - * * @throws \Kdyby\Events\InvalidListenerException */ public function addEventListener($events, $subscriber, $priority = 0) { foreach ((array) $events as $eventName) { - list($namespace, $event) = Event::parseName($eventName); + [, $event] = Event::parseName($eventName); if (!$subscriber instanceof Closure) { $callback = !is_array($subscriber) ? [$subscriber, $event] : $subscriber; if ($callback[0] instanceof CallableSubscriber) { if (!is_callable($callback)) { - throw new \Kdyby\Events\InvalidListenerException(sprintf('Event listener "%s" is not callable.', $callback[0])); + throw new \Kdyby\Events\InvalidListenerException(sprintf('Event listener "%s" is not callable.', get_class($callback[0]))); } } elseif (!method_exists($callback[0], $callback[1])) { @@ -188,9 +189,9 @@ public function addEventListener($events, $subscriber, $priority = 0) public function removeEventListener($unsubscribe, $subscriber = NULL) { if ($unsubscribe instanceof EventSubscriber) { - list($unsubscribe, $subscriber) = $this->extractSubscriber($unsubscribe); + [$unsubscribe, $subscriber] = $this->extractSubscriber($unsubscribe); } elseif ($unsubscribe instanceof Closure) { - list($unsubscribe, $subscriber) = $this->extractCallable($unsubscribe); + [$unsubscribe, $subscriber] = $this->extractCallable($unsubscribe); } foreach ((array) $unsubscribe as $eventName) { @@ -290,11 +291,11 @@ public function addEventSubscriber(EventSubscriber $subscriber) $this->addEventListener($eventName, [$subscriber, $params]); } elseif (is_string($params[0])) { // [EventName => [method, priority], ...] - $this->addEventListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0); + $this->addEventListener($eventName, [$subscriber, $params[0]], $params[1] ?? 0); } else { foreach ($params as $listener) { // [EventName => [[method, priority], ...], ...] - $this->addEventListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0); + $this->addEventListener($eventName, [$subscriber, $listener[0]], $listener[1] ?? 0); } } } @@ -310,8 +311,8 @@ public function removeEventSubscriber(EventSubscriber $subscriber) } /** - * @param string|array $name - * @param array $defaults + * @param string $name + * @param array|\Traversable|null $defaults * @param string $argsClass * @param bool $globalDispatchFirst * @return \Kdyby\Events\Event @@ -334,7 +335,7 @@ private function sortListeners($eventName) $this->sorted[$eventName] = []; $available = []; - list($namespace, $event, $separator) = Event::parseName($eventName); + [$namespace, $event, $separator] = Event::parseName($eventName); $className = $namespace; do { $key = ($className ? $className . $separator : '') . $event; @@ -342,7 +343,7 @@ private function sortListeners($eventName) continue; } - $available = $available + array_fill_keys(array_keys($this->listeners[$key]), []); + $available += array_fill_keys(array_keys($this->listeners[$key]), []); foreach ($this->listeners[$key] as $priority => $listeners) { foreach ($listeners as $listener) { if ($className === $namespace && in_array($listener, $available[$priority], TRUE)) { @@ -360,9 +361,9 @@ private function sortListeners($eventName) } krsort($available); // [priority => [[listener, ...], ...] - $sorted = call_user_func_array('array_merge', $available); + $sorted = array_merge(...$available); - $this->sorted[$eventName] = array_map(function ($callable) use ($event) { + $this->sorted[$eventName] = array_map(static function ($callable) use ($event) { if ($callable instanceof EventSubscriber) { return $callable; } diff --git a/src/Events/LazyEventManager.php b/src/Events/LazyEventManager.php index 1bbf231..e635c08 100644 --- a/src/Events/LazyEventManager.php +++ b/src/Events/LazyEventManager.php @@ -22,7 +22,7 @@ class LazyEventManager extends \Kdyby\Events\EventManager { /** - * @var array + * @var array */ private $listenerIds; @@ -32,7 +32,7 @@ class LazyEventManager extends \Kdyby\Events\EventManager private $container; /** - * @param array $listenerIds + * @param array $listenerIds * @param \Nette\DI\Container $container */ public function __construct(array $listenerIds, DIContainer $container) @@ -70,9 +70,9 @@ public function getListeners($eventName = NULL) public function removeEventListener($unsubscribe, $subscriber = NULL) { if ($unsubscribe instanceof EventSubscriber) { - list($unsubscribe, $subscriber) = $this->extractSubscriber($unsubscribe); + [$unsubscribe, $subscriber] = $this->extractSubscriber($unsubscribe); } elseif ($unsubscribe instanceof Closure) { - list($unsubscribe, $subscriber) = $this->extractCallable($unsubscribe); + [$unsubscribe, $subscriber] = $this->extractCallable($unsubscribe); } foreach ((array) $unsubscribe as $eventName) { diff --git a/src/Events/NamespacedEventManager.php b/src/Events/NamespacedEventManager.php index f7e1a45..fc94b05 100644 --- a/src/Events/NamespacedEventManager.php +++ b/src/Events/NamespacedEventManager.php @@ -42,7 +42,7 @@ public function __construct($namespace, EventManager $eventManager) */ public function dispatchEvent($eventName, DoctrineEventArgs $eventArgs = NULL) { - list($ns, $event) = Event::parseName($eventName); + [$ns, $event] = Event::parseName($eventName); $this->evm->dispatchEvent($ns !== NULL ? $eventName : $this->namespace . $eventName, $eventArgs); @@ -59,7 +59,7 @@ public function getListeners($eventName = NULL) if ($eventName === NULL) { $listeners = []; foreach ($this->evm->getListeners(NULL) as $subscriberEventName => $subscribers) { - list($ns, $event) = Event::parseName($subscriberEventName); + [$ns, $event] = Event::parseName($subscriberEventName); if ($ns === NULL || stripos($this->namespace, $ns) !== FALSE) { $listeners[$subscriberEventName] = $subscribers; } @@ -68,7 +68,7 @@ public function getListeners($eventName = NULL) return $listeners; } - list($ns, $event) = Event::parseName($eventName); + [$ns, $event] = Event::parseName($eventName); if ($ns !== NULL) { throw new \Kdyby\Events\InvalidArgumentException('Unexpected event with namespace.'); @@ -85,7 +85,7 @@ public function getListeners($eventName = NULL) */ public function hasListeners($eventName) { - list($ns, $event) = Event::parseName($eventName); + [$ns, $event] = Event::parseName($eventName); if ($ns) { return $this->evm->hasListeners($eventName) || $this->evm->hasListeners($event); @@ -100,7 +100,7 @@ public function hasListeners($eventName) public function addEventListener($events, $subscriber, $priority = 0) { foreach ((array) $events as $eventName) { - list($ns, $event) = Event::parseName($eventName); + [$ns, $event] = Event::parseName($eventName); $this->evm->addEventListener([$ns === NULL ? $this->namespace . $event : $eventName], $subscriber); } } @@ -126,10 +126,9 @@ public function removeEventListener($unsubscribe, $subscriber = NULL) } } - $namespace = $this->namespace; - $unsubscribe = array_map(function ($eventName) use ($namespace) { - list($ns, $event) = Event::parseName($eventName); - return $ns === NULL ? $namespace . $event : $eventName; + $unsubscribe = array_map(function ($eventName) { + [$ns, $event] = Event::parseName($eventName); + return $ns === NULL ? $this->namespace . $event : $eventName; }, (array) $unsubscribe); $this->evm->removeEventListener($unsubscribe, $subscriber); diff --git a/tests/KdybyTests/Events/EventManagerTest.phpt b/tests/KdybyTests/Events/EventManagerTest.phpt index 7bd83a2..6dae87a 100644 --- a/tests/KdybyTests/Events/EventManagerTest.phpt +++ b/tests/KdybyTests/Events/EventManagerTest.phpt @@ -152,7 +152,7 @@ class EventManagerTest extends \Tester\TestCase $evm = $this->manager; $listener = new EventListenerMock(); - Assert::exception(function () use ($evm, $listener) { + Assert::exception(static function () use ($evm, $listener) { $evm->addEventListener('onNonexisting', $listener); }, \Kdyby\Events\InvalidListenerException::class); } @@ -193,7 +193,7 @@ class EventManagerTest extends \Tester\TestCase public function testDispatchingCallable() { $triggerCounter = 0; - $callback = function () use (& $triggerCounter) { + $callback = static function () use (& $triggerCounter) { $triggerCounter++; }; @@ -244,7 +244,6 @@ class EventManagerTest extends \Tester\TestCase /** * @dataProvider dataEventsDispatchingNamespaces - * * @param string $trigger * @param array $called */ @@ -459,7 +458,7 @@ class EventManagerTest extends \Tester\TestCase */ private static function getEmptyClosure() { - return function () { + return static function () { }; } diff --git a/tests/KdybyTests/Events/EventTest.phpt b/tests/KdybyTests/Events/EventTest.phpt index f4a89a1..0a8bffd 100644 --- a/tests/KdybyTests/Events/EventTest.phpt +++ b/tests/KdybyTests/Events/EventTest.phpt @@ -47,10 +47,10 @@ class EventTest extends \Tester\TestCase $foo = new FooMock(); $foo->onBar = new Event('bar'); - $foo->onBar[] = function ($lorem) use (&$calls) { + $foo->onBar[] = static function ($lorem) use (&$calls) { $calls[] = [__METHOD__, func_get_args()]; }; - $foo->onBar[] = function ($lorem) use (&$calls) { + $foo->onBar[] = static function ($lorem) use (&$calls) { $calls[] = [__METHOD__, func_get_args()]; }; @@ -98,7 +98,7 @@ class EventTest extends \Tester\TestCase $foo->onMagic->injectEventManager($evm); // listener - $foo->onMagic[] = function (FooMock $foo, $int) use (&$calls) { + $foo->onMagic[] = static function (FooMock $foo, $int) use (&$calls) { $calls[] = [__METHOD__, func_get_args()]; }; @@ -107,7 +107,7 @@ class EventTest extends \Tester\TestCase $foo->onStartup->injectEventManager($evm); // listener - $foo->onStartup[] = function (FooMock $foo, $int) use (&$calls) { + $foo->onStartup[] = static function (FooMock $foo, $int) use (&$calls) { $calls[] = [__METHOD__, func_get_args()]; }; @@ -116,8 +116,8 @@ class EventTest extends \Tester\TestCase public function testDispatchToManagerInvoke() { - $foo = new FooMock; - $listener = new LoremListener; + $foo = new FooMock(); + $listener = new LoremListener(); $this->dataToManagerDispatch($foo, $listener, $calls); $foo->onMagic($foo, 2); @@ -133,8 +133,8 @@ class EventTest extends \Tester\TestCase public function testDispatchToManagerDispatch() { - $foo = new FooMock; - $listener = new LoremListener; + $foo = new FooMock(); + $listener = new LoremListener(); $this->dataToManagerDispatch($foo, $listener, $calls); $foo->onMagic->dispatch([$foo, 3]); @@ -150,8 +150,8 @@ class EventTest extends \Tester\TestCase public function testDispatchToManagerSecondInvoke() { - $foo = new FooMock; - $listener = new LoremListener; + $foo = new FooMock(); + $listener = new LoremListener(); $this->dataToManagerDispatch($foo, $listener, $calls); $foo->onStartup($foo, 4); @@ -161,9 +161,9 @@ class EventTest extends \Tester\TestCase Assert::same([$foo, 4], $calls[0][1]); Assert::same(1, count($listener->calls)); - list($call) = $listener->calls; + [$call] = $listener->calls; Assert::same(LoremListener::class . '::onStartup', $call[0]); - list($args) = $call[1]; + [$args] = $call[1]; Assert::true($args instanceof StartupEventArgs); Assert::same($foo, $args->foo); Assert::same(4, $args->int); @@ -179,7 +179,7 @@ class EventTest extends \Tester\TestCase $event->injectEventManager($evm); $event->globalDispatchFirst = TRUE; - $event[] = function () use ($listener) { + $event[] = static function () use ($listener) { $listener->calls[] = __METHOD__; }; @@ -202,7 +202,7 @@ class EventTest extends \Tester\TestCase $event->injectEventManager($evm); $event->globalDispatchFirst = FALSE; - $event[] = function () use ($listener) { + $event[] = static function () use ($listener) { $listener->calls[] = __METHOD__; }; diff --git a/tests/KdybyTests/Events/ExtensionTest.phpt b/tests/KdybyTests/Events/ExtensionTest.phpt index 7edf99d..c570c2b 100644 --- a/tests/KdybyTests/Events/ExtensionTest.phpt +++ b/tests/KdybyTests/Events/ExtensionTest.phpt @@ -60,19 +60,15 @@ class ExtensionTest extends \Tester\TestCase public function testValidateDirect() { - $me = $this; - - Assert::exception(function () use ($me) { - $me->createContainer('validate.direct'); + Assert::exception(function () { + $this->createContainer('validate.direct'); }, \Nette\Utils\AssertionException::class, 'Please, do not register listeners directly to service @events.manager. %a%'); } public function testValidateMissing() { - $me = $this; - try { - $me->createContainer('validate.missing'); + $this->createContainer('validate.missing'); Assert::fail('Expected exception'); } catch (\Nette\Utils\AssertionException $e) { @@ -91,28 +87,22 @@ class ExtensionTest extends \Tester\TestCase public function testValidateFake() { - $me = $this; - - Assert::exception(function () use ($me) { - $me->createContainer('validate.fake'); + Assert::exception(function () { + $this->createContainer('validate.fake'); }, \Nette\Utils\AssertionException::class, 'Subscriber @events.subscriber.%a% doesn\'t implement Kdyby\Events\Subscriber.'); } public function testValidateInvalid() { - $me = $this; - - Assert::exception(function () use ($me) { - $me->createContainer('validate.invalid'); + Assert::exception(function () { + $this->createContainer('validate.invalid'); }, \Nette\Utils\AssertionException::class, 'Event listener KdybyTests\Events\FirstInvalidListenerMock::onFoo() is not implemented.'); } public function testValidateInvalid2() { - $me = $this; - - Assert::exception(function () use ($me) { - $me->createContainer('validate.invalid2'); + Assert::exception(function () { + $this->createContainer('validate.invalid2'); }, \Nette\Utils\AssertionException::class, 'Event listener KdybyTests\Events\SecondInvalidListenerMock::onBar() is not implemented.'); } @@ -257,8 +247,8 @@ class ExtensionTest extends \Tester\TestCase Assert::false($container->isCreated('baz')); Assert::same(1, count($manager->getListeners('onStartup'))); - $baz = $container->getService('foo'); /** @var \KdybyTests\Events\NamespacedEventListenerMock $baz */ + $baz = $container->getService('foo'); Assert::same([ [LoremListener::class . '::onStartup', [$bazArgs]], @@ -304,8 +294,7 @@ class ExtensionTest extends \Tester\TestCase public function testGlobalDispatchFirst() { $container = $this->createContainer('globalDispatchFirst'); - $manager = $container->getService('events.manager'); - /** @var \Kdyby\Events\EventManager $manager */ + $container->getService('events.manager'); $mock = $container->getService('dispatchOrderMock'); Assert::true($mock->onGlobalDispatchFirst->globalDispatchFirst); @@ -316,8 +305,7 @@ class ExtensionTest extends \Tester\TestCase public function testGlobalDispatchLast() { $container = $this->createContainer('globalDispatchLast'); - $manager = $container->getService('events.manager'); - /** @var \Kdyby\Events\EventManager $manager */ + $container->getService('events.manager'); $mock = $container->getService('dispatchOrderMock'); Assert::true($mock->onGlobalDispatchFirst->globalDispatchFirst); diff --git a/tests/KdybyTests/Events/IExceptionHandlerTest.phpt b/tests/KdybyTests/Events/IExceptionHandlerTest.phpt index 2bf4ec7..b944da9 100644 --- a/tests/KdybyTests/Events/IExceptionHandlerTest.phpt +++ b/tests/KdybyTests/Events/IExceptionHandlerTest.phpt @@ -27,9 +27,8 @@ class IExceptionHandlerTest extends \Tester\TestCase public function testNotCaught() { - $evm = $this->evm; - Assert::exception(function () use ($evm) { - $evm->dispatchEvent('testEvent'); + Assert::exception(function () { + $this->evm->dispatchEvent('testEvent'); }, \Exception::class); } diff --git a/tests/KdybyTests/Events/data/FooMock.php b/tests/KdybyTests/Events/data/FooMock.php index 3e161d5..7338cb4 100644 --- a/tests/KdybyTests/Events/data/FooMock.php +++ b/tests/KdybyTests/Events/data/FooMock.php @@ -4,8 +4,8 @@ /** * @method onBar($lorem) - * @method onMagic(FooMock $foo, $int) - * @method onStartup(FooMock $foo, $int) + * @method onMagic(\KdybyTests\Events\FooMock $foo, $int) + * @method onStartup(\KdybyTests\Events\FooMock $foo, $int) */ class FooMock { diff --git a/tests/KdybyTests/Events/data/InheritSubscriber.php b/tests/KdybyTests/Events/data/InheritSubscriber.php index dd2ee85..ac714a7 100644 --- a/tests/KdybyTests/Events/data/InheritSubscriber.php +++ b/tests/KdybyTests/Events/data/InheritSubscriber.php @@ -27,7 +27,7 @@ public function onCreate() { $backtrace = debug_backtrace(); $event = $backtrace[2]['args'][0]; - $this->eventCalls[$event] = 1 + (isset($this->eventCalls[$event]) ? $this->eventCalls[$event] : 0); + $this->eventCalls[$event] = 1 + ($this->eventCalls[$event] ?? 0); } } diff --git a/tests/KdybyTests/Events/data/ListenersContainer.php b/tests/KdybyTests/Events/data/ListenersContainer.php index 9f65d17..6ebe14e 100644 --- a/tests/KdybyTests/Events/data/ListenersContainer.php +++ b/tests/KdybyTests/Events/data/ListenersContainer.php @@ -22,7 +22,7 @@ protected function createServiceThird() protected function createServiceFourth() { - return function () { + return static function () { }; } diff --git a/tests/KdybyTests/Events/data/MagicEventListenerMock.php b/tests/KdybyTests/Events/data/MagicEventListenerMock.php index 77ae564..d4a0f75 100644 --- a/tests/KdybyTests/Events/data/MagicEventListenerMock.php +++ b/tests/KdybyTests/Events/data/MagicEventListenerMock.php @@ -26,8 +26,8 @@ public function getSubscribedEvents() public function __call($name, $arguments) { $args = $arguments[0]; - $args->calls[] = [__CLASS__ . '::' . $name, $arguments]; - $this->calls[] = [__CLASS__ . '::' . $name, $arguments]; + $args->calls[] = [self::class . '::' . $name, $arguments]; + $this->calls[] = [self::class . '::' . $name, $arguments]; } } diff --git a/tests/KdybyTests/Events/data/SecondInheritSubscriber.php b/tests/KdybyTests/Events/data/SecondInheritSubscriber.php index f4feaa5..2feb764 100644 --- a/tests/KdybyTests/Events/data/SecondInheritSubscriber.php +++ b/tests/KdybyTests/Events/data/SecondInheritSubscriber.php @@ -32,7 +32,7 @@ public function onCreate() $this->eventCalls['unknown'] += 1; } else { $eventName = $event['args'][0]; - $this->eventCalls[$eventName] = 1 + (isset($this->eventCalls[$eventName]) ? $this->eventCalls[$eventName] : 0); + $this->eventCalls[$eventName] = 1 + ($this->eventCalls[$eventName] ?? 0); } } diff --git a/tests/KdybyTests/bootstrap.php b/tests/KdybyTests/bootstrap.php index a1667d4..e522bb6 100755 --- a/tests/KdybyTests/bootstrap.php +++ b/tests/KdybyTests/bootstrap.php @@ -36,4 +36,7 @@ 'argv', ])); $_SERVER['REQUEST_TIME'] = 1234567890; + +// phpcs:disable SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable $_ENV = $_GET = $_POST = []; +// phpcs:enable SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable