From fe363932904607864981715864df6feb0e0daa70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=BDurek?= Date: Tue, 19 Mar 2019 21:54:09 +0100 Subject: [PATCH 1/2] travis: run on php 7.3, udpate phpstan --- .travis.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa40ddc..fac1ebd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ php: - 7.0 - 7.1 - 7.2 + - 7.3 env: - # dev @@ -20,15 +21,15 @@ 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: From cf58d3ab4ce4eb5872e821cd6fe8bc981509fc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=BDurek?= Date: Tue, 19 Mar 2019 22:45:45 +0100 Subject: [PATCH 2/2] phpstan 0.11 --- .travis.yml | 3 +-- composer.json | 3 ++- phpstan.neon | 3 --- phpstan.neon.dist | 5 +++++ src/Events/DI/EventsExtension.php | 24 +++++++++++++----------- src/Events/Diagnostics/Panel.php | 14 +++++++------- src/Events/Event.php | 11 +++++++---- src/Events/EventManager.php | 24 +++++++++++++----------- src/Events/LazyEventManager.php | 4 ++-- 9 files changed, 50 insertions(+), 41 deletions(-) delete mode 100644 phpstan.neon create mode 100644 phpstan.neon.dist diff --git a/.travis.yml b/.travis.yml index fac1ebd..9e81569 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,7 +34,6 @@ matrix: 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 @@ -42,7 +41,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..c8d8b05 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,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..e6ad0d0 100644 --- a/src/Events/DI/EventsExtension.php +++ b/src/Events/DI/EventsExtension.php @@ -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,30 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit ) ); - } elseif (!class_exists($def->getClass())) { + } elseif (!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); $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 +279,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])); } } } diff --git a/src/Events/Diagnostics/Panel.php b/src/Events/Diagnostics/Panel.php index 16de42b..240a0fb 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 . '
'; } @@ -343,7 +343,7 @@ private function renderListeners($ids) { static $addIcon; if (empty($addIcon)) { - $addIcon = ''; + $addIcon = ''; } $registeredClasses = $this->getClassMap(); @@ -351,10 +351,10 @@ private function renderListeners($ids) $h = 'htmlspecialchars'; $shortFilename = function (ReflectionFunctionAbstract $refl) { - $title = '.../' . basename($refl->getFileName()) . ':' . $refl->getStartLine(); + $title = '.../' . basename((string) $refl->getFileName()) . ':' . $refl->getStartLine(); /** @var string|NULL $editor */ - $editor = TracyHelpers::editorUri($refl->getFileName(), $refl->getStartLine()); + $editor = TracyHelpers::editorUri((string) $refl->getFileName(), (int) $refl->getStartLine()); if ($editor !== NULL) { return sprintf(' defined at %s', htmlspecialchars($editor), $title); } @@ -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..0b9ccaa 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -46,7 +46,7 @@ class Event implements \ArrayAccess, \IteratorAggregate, \Countable private $namespace; /** - * @var \Kdyby\Events\EventManager + * @var \Kdyby\Events\EventManager|null */ private $eventManager; @@ -56,13 +56,13 @@ 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) @@ -162,6 +162,7 @@ public function getListeners() $name = $this->getName(); $evm = $this->eventManager; + assert($evm !== null); $argsClass = $this->argsClass; $globalDispatch = function () use ($name, $evm, $argsClass) { if ($argsClass === NULL) { @@ -171,6 +172,8 @@ public function getListeners() $args = ClassTypeReflection::from($argsClass)->newInstanceArgs(func_get_args()); } + assert($args instanceof \Doctrine\Common\EventArgs); + $evm->dispatchEvent($name, $args); }; @@ -193,7 +196,7 @@ public function __invoke() } /** - * @param string $name + * @param string|array $name * @return array */ public static function parseName(&$name) diff --git a/src/Events/EventManager.php b/src/Events/EventManager.php index a53f603..ccf0dbc 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; @@ -90,6 +90,8 @@ public function dispatchEvent($eventName, DoctrineEventArgs $eventArgs = NULL) $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) @@ -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) @@ -166,7 +168,7 @@ public function addEventListener($events, $subscriber, $priority = 0) $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])) { @@ -310,8 +312,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 @@ -360,7 +362,7 @@ 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) { if ($callable instanceof EventSubscriber) { diff --git a/src/Events/LazyEventManager.php b/src/Events/LazyEventManager.php index 1bbf231..b9e7383 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)