Skip to content

Commit

Permalink
Merge d173085 into 57edd92
Browse files Browse the repository at this point in the history
  • Loading branch information
adaamz committed Mar 21, 2019
2 parents 57edd92 + d173085 commit d1d10d4
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 130 deletions.
15 changes: 6 additions & 9 deletions .travis.yml
Expand Up @@ -7,10 +7,9 @@ cache:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

env:
- # dev
Expand All @@ -20,28 +19,26 @@ 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

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:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon

This file was deleted.

5 changes: 5 additions & 0 deletions phpstan.neon.dist
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
- tests/KdybyTests
37 changes: 21 additions & 16 deletions src/Events/DI/EventsExtension.php
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -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.',
Expand All @@ -246,49 +248,51 @@ 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 => ???, ...]
$eventNames[] = ltrim($eventName, '\\');

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]));
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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());
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/Events/Diagnostics/Panel.php
Expand Up @@ -70,7 +70,7 @@ class Panel implements \Tracy\IBarPanel
private $registeredClasses;

/**
* @var bool
* @var bool|array<string, mixed>
*/
public $renderPanel = TRUE;

Expand Down Expand Up @@ -151,7 +151,7 @@ public function getTab()
}

return '<span title="Kdyby/Events">'
. '<img width="16" height="16" src="data:image/png;base64,' . base64_encode(file_get_contents(__DIR__ . '/icon.png')) . '" />'
. '<img width="16" height="16" src="data:image/png;base64,' . base64_encode((string) file_get_contents(__DIR__ . '/icon.png')) . '" />'
. '<span class="tracy-label">' . count(Arrays::flatten($this->dispatchLog)) . ' calls</span>'
. '</span>';
}
Expand Down Expand Up @@ -190,7 +190,7 @@ public function getPanel()
$totalListeners = count(array_unique(Arrays::flatten($this->listenerIds)));

return '<style>' . $this->renderStyles() . '</style>' .
'<h1>' . $h($totalEvents) . ' registered events, ' . $h($totalListeners) . ' registered listeners</h1>' .
'<h1>' . $h((string) $totalEvents) . ' registered events, ' . $h((string) $totalListeners) . ' registered listeners</h1>' .
'<div class="nette-inner tracy-inner nette-KdybyEventsPanel"><table>' . $s . '</table></div>';
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -425,7 +425,7 @@ private function renderCalls(array $calls)
{
static $runIcon;
if (empty($runIcon)) {
$runIcon = '<img width="18" height="18" src="data:image/png;base64,' . base64_encode(file_get_contents(__DIR__ . '/run.png')) . '" title="Event dispatch" />';
$runIcon = '<img width="18" height="18" src="data:image/png;base64,' . base64_encode((string) file_get_contents(__DIR__ . '/run.png')) . '" title="Event dispatch" />';
}

$s = '';
Expand Down
19 changes: 11 additions & 8 deletions src/Events/Event.php
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,7 @@ class Event implements \ArrayAccess, \IteratorAggregate, \Countable
private $namespace;

/**
* @var \Kdyby\Events\EventManager
* @var \Kdyby\Events\EventManager|null
*/
private $eventManager;

Expand All @@ -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) {
Expand Down Expand Up @@ -162,15 +163,18 @@ 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());

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

assert($args instanceof EventArgs);

$evm->dispatchEvent($name, $args);
};

Expand All @@ -193,7 +197,7 @@ public function __invoke()
}

/**
* @param string $name
* @param string|array $name
* @return array
*/
public static function parseName(&$name)
Expand Down Expand Up @@ -271,15 +275,14 @@ 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];
}

/**
* @param int $index
*
* @return bool
*/
public function offsetExists($index)
Expand Down

0 comments on commit d1d10d4

Please sign in to comment.