From 5cbf4e77d2fbe1594d9a63927c5c85cffe50f3e6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 3 Mar 2021 22:36:11 +0100 Subject: [PATCH 01/14] removed ecs.php --- ecs.php | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 ecs.php diff --git a/ecs.php b/ecs.php deleted file mode 100644 index 644dd7d69..000000000 --- a/ecs.php +++ /dev/null @@ -1,21 +0,0 @@ -import(PRESET_DIR . '/php71.php'); - - $parameters = $containerConfigurator->parameters(); - - $parameters->set('skip', [ - PhpCsFixer\Fixer\Basic\Psr4Fixer::class => [ - 'tests/bootstrap.php', - ], - ]); -}; From b8098b3812e36a6e53862bc75a67930bc69b22de Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 31 Dec 2020 20:04:46 +0100 Subject: [PATCH 02/14] opened 4.0-dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9a29710cd..2844deda0 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "4.0-dev" } } } From c235c3bf0fcdb2ef734a92ac524a6d02322936b1 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 31 Dec 2020 20:05:06 +0100 Subject: [PATCH 03/14] requires PHP 8.0 --- .github/workflows/coding-style.yml | 2 +- .github/workflows/static-analysis.yml | 2 +- .github/workflows/tests.yml | 6 +++--- composer.json | 2 +- readme.md | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/coding-style.yml b/.github/workflows/coding-style.yml index 337a0a631..00abb989e 100644 --- a/.github/workflows/coding-style.yml +++ b/.github/workflows/coding-style.yml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 7.1 + php-version: 8.0 coverage: none - run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index b0692d716..f985b0526 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.0 coverage: none - run: composer install --no-progress --prefer-dist diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 73516285b..7a5fee587 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4', '8.0'] + php: ['8.0'] fail-fast: false @@ -35,7 +35,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 7.1 + php-version: 8.0 coverage: none - run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable @@ -49,7 +49,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.0 coverage: none - run: composer install --no-progress --prefer-dist diff --git a/composer.json b/composer.json index 2844deda0..30dcab9a6 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=7.1 <8.1", + "php": ">=8.0 <8.1", "ext-tokenizer": "*", "nette/neon": "^3.0", "nette/php-generator": "^3.3.3", diff --git a/readme.md b/readme.md index aa8ae6402..5b1e3cb13 100644 --- a/readme.md +++ b/readme.md @@ -37,7 +37,7 @@ The recommended way to install is via Composer: composer require nette/di ``` -It requires PHP version 7.1 and supports PHP up to 8.0. +It requires PHP version 8.0. Usage From 9ff235bdd4508a29e689801594c2820b8e24ee71 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 16:44:22 +0100 Subject: [PATCH 04/14] composer: updated dependencies --- composer.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 30dcab9a6..e803ebffe 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,15 @@ "require": { "php": ">=8.0 <8.1", "ext-tokenizer": "*", - "nette/neon": "^3.0", - "nette/php-generator": "^3.3.3", - "nette/robot-loader": "^3.2", + "nette/neon": "^3.0 || ^4.0", + "nette/php-generator": "^3.3.3 || ^4.0", + "nette/robot-loader": "^3.3.1 || ^4.0", "nette/schema": "^1.1", - "nette/utils": "^3.2" + "nette/utils": "^3.2 || ^4.0" }, "require-dev": { - "nette/tester": "^2.2", - "tracy/tracy": "^2.3", + "nette/tester": "^2.4", + "tracy/tracy": "^2.8", "phpstan/phpstan": "^0.12" }, "conflict": { From eb0f270621b2ab4de14bf825745ab4137e2c6136 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 31 Dec 2020 20:04:30 +0100 Subject: [PATCH 05/14] PhpExtension & ConstantsExtension are deprecated --- src/DI/Extensions/ConstantsExtension.php | 1 + src/DI/Extensions/PhpExtension.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/DI/Extensions/ConstantsExtension.php b/src/DI/Extensions/ConstantsExtension.php index ab94e8766..7fd5e9630 100644 --- a/src/DI/Extensions/ConstantsExtension.php +++ b/src/DI/Extensions/ConstantsExtension.php @@ -14,6 +14,7 @@ /** * Constant definitions. + * @deprecated */ final class ConstantsExtension extends Nette\DI\CompilerExtension { diff --git a/src/DI/Extensions/PhpExtension.php b/src/DI/Extensions/PhpExtension.php index 6a469e32f..bb8ef5265 100644 --- a/src/DI/Extensions/PhpExtension.php +++ b/src/DI/Extensions/PhpExtension.php @@ -14,6 +14,7 @@ /** * PHP directives definition. + * @deprecated */ final class PhpExtension extends Nette\DI\CompilerExtension { From 48c0eaf77536fa3838e8c08de5b29f4f75677575 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 12 Jan 2021 16:06:00 +0100 Subject: [PATCH 06/14] removed deprecated stuff --- src/DI/Compiler.php | 14 ---- src/DI/Config/DefinitionSchema.php | 14 ++-- src/DI/Config/Helpers.php | 9 +-- src/DI/ContainerBuilder.php | 8 --- src/DI/Definitions/FactoryDefinition.php | 75 --------------------- src/DI/Definitions/ServiceDefinition.php | 51 -------------- src/DI/Extensions/DIExtension.php | 11 --- tests/DI/DIExtension.run.phpt | 32 --------- tests/DI/Definitions.ServiceDefinition.phpt | 23 ------- 9 files changed, 6 insertions(+), 231 deletions(-) delete mode 100644 tests/DI/DIExtension.run.phpt diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index 281d53123..5a8ceaacf 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -318,18 +318,4 @@ protected function createPhpGenerator(): PhpGenerator { return new PhpGenerator($this->builder); } - - - /** @deprecated use non-static Compiler::loadDefinitionsFromConfig() */ - public static function loadDefinitions(): void - { - throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated, use non-static Compiler::loadDefinitionsFromConfig(array $configList).'); - } - - - /** @deprecated use non-static Compiler::loadDefinitionsFromConfig() */ - public static function loadDefinition(): void - { - throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated, use non-static Compiler::loadDefinitionsFromConfig(array $configList).'); - } } diff --git a/src/DI/Config/DefinitionSchema.php b/src/DI/Config/DefinitionSchema.php index e1a394cbf..7d30195fa 100644 --- a/src/DI/Config/DefinitionSchema.php +++ b/src/DI/Config/DefinitionSchema.php @@ -92,16 +92,10 @@ public function normalize($def, Context $context) return ['factory' => $def]; } elseif (is_array($def)) { - if (isset($def['class']) && !isset($def['type'])) { - if ($def['class'] instanceof Statement) { - $key = end($context->path); - trigger_error("Service '$key': option 'class' should be changed to 'factory'.", E_USER_DEPRECATED); - $def['factory'] = $def['class']; - unset($def['class']); - } elseif (!isset($def['factory']) && !isset($def['dynamic']) && !isset($def['imported'])) { - $def['factory'] = $def['class']; - unset($def['class']); - } + // back compatibility + if (isset($def['class']) && !isset($def['type']) && !isset($def['factory']) && !isset($def['dynamic']) && !isset($def['imported'])) { + $def['factory'] = $def['class']; + unset($def['class']); } foreach (['class' => 'type', 'dynamic' => 'imported'] as $alias => $original) { diff --git a/src/DI/Config/Helpers.php b/src/DI/Config/Helpers.php index ec1528482..273c71be6 100644 --- a/src/DI/Config/Helpers.php +++ b/src/DI/Config/Helpers.php @@ -23,19 +23,14 @@ final class Helpers public const PREVENT_MERGING = '_prevent_merging'; - /** - * Merges configurations. Left has higher priority than right one. - * @return array|string - */ + /** @deprecated */ public static function merge($left, $right) { return Nette\Schema\Helpers::merge($left, $right); } - /** - * Return true if array prevents merging and removes this information. - */ + /** @deprecated */ public static function takeParent(&$data): bool { if (is_array($data) && isset($data[self::PREVENT_MERGING])) { diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 7b1f0e76c..5926a011d 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -406,12 +406,4 @@ public function formatPhp(string $statement, array $args): string }); return (new PhpGenerator($this))->formatPhp($statement, $args); } - - - /** @deprecated use resolve() */ - public function prepareClassList(): void - { - trigger_error(__METHOD__ . '() is deprecated, use resolve()', E_USER_DEPRECATED); - $this->resolve(); - } } diff --git a/src/DI/Definitions/FactoryDefinition.php b/src/DI/Definitions/FactoryDefinition.php index 362c397e3..3303163c8 100644 --- a/src/DI/Definitions/FactoryDefinition.php +++ b/src/DI/Definitions/FactoryDefinition.php @@ -76,81 +76,6 @@ public function getResultDefinition(): Definition } - /** - * @deprecated use ->getResultDefinition()->setFactory() - * @return static - */ - public function setFactory($factory, array $args = []) - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->setFactory()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - $this->resultDefinition->setFactory($factory, $args); - return $this; - } - - - /** @deprecated use ->getResultDefinition()->getFactory() */ - public function getFactory(): ?Statement - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->getFactory()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - return $this->resultDefinition->getFactory(); - } - - - /** - * @deprecated use ->getResultDefinition()->getEntity() - * @return mixed - */ - public function getEntity() - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->getEntity()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - return $this->resultDefinition->getEntity(); - } - - - /** - * @deprecated use ->getResultDefinition()->setArguments() - * @return static - */ - public function setArguments(array $args = []) - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->setArguments()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - $this->resultDefinition->setArguments($args); - return $this; - } - - - /** - * @deprecated use ->getResultDefinition()->setSetup() - * @return static - */ - public function setSetup(array $setup) - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->setSetup()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - $this->resultDefinition->setSetup($setup); - return $this; - } - - - /** @deprecated use ->getResultDefinition()->getSetup() */ - public function getSetup(): array - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->getSetup()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - return $this->resultDefinition->getSetup(); - } - - - /** - * @deprecated use ->getResultDefinition()->addSetup() - * @return static - */ - public function addSetup($entity, array $args = []) - { - trigger_error(sprintf('Service %s: %s() is deprecated, use ->getResultDefinition()->addSetup()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - $this->resultDefinition->addSetup($entity, $args); - return $this; - } - - /** @return static */ public function setParameters(array $params) { diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index dc00d6344..7ea55b04a 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -36,20 +36,6 @@ public function __construct() } - /** @deprecated Use setType() */ - public function setClass(?string $type) - { - $this->setType($type); - if (func_num_args() > 1) { - trigger_error(sprintf('Service %s: %s() second parameter $args is deprecated, use setFactory()', $this->getName(), __METHOD__), E_USER_DEPRECATED); - if ($args = func_get_arg(1)) { - $this->setFactory($type, $args); - } - } - return $this; - } - - /** @return static */ public function setType(?string $type) { @@ -135,43 +121,6 @@ public function addSetup($entity, array $args = []) } - /** @deprecated */ - public function setParameters(array $params) - { - throw new Nette\DeprecatedException(sprintf('Service %s: %s() is deprecated.', $this->getName(), __METHOD__)); - } - - - /** @deprecated */ - public function getParameters(): array - { - trigger_error(sprintf('Service %s: %s() is deprecated.', $this->getName(), __METHOD__), E_USER_DEPRECATED); - return []; - } - - - /** @deprecated use $builder->addImportedDefinition(...) */ - public function setDynamic(): void - { - throw new Nette\DeprecatedException(sprintf('Service %s: %s() is deprecated, use $builder->addImportedDefinition(...)', $this->getName(), __METHOD__)); - } - - - /** @deprecated use $builder->addFactoryDefinition(...) or addAccessorDefinition(...) */ - public function setImplement(): void - { - throw new Nette\DeprecatedException(sprintf('Service %s: %s() is deprecated, use $builder->addFactoryDefinition(...)', $this->getName(), __METHOD__)); - } - - - /** @deprecated use addTag('nette.inject') */ - public function setInject(bool $state = true) - { - trigger_error(sprintf('Service %s: %s() is deprecated, use addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT)', $this->getName(), __METHOD__), E_USER_DEPRECATED); - return $this->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT, $state); - } - - public function resolveType(Nette\DI\Resolver $resolver): void { if (!$this->getEntity()) { diff --git a/src/DI/Extensions/DIExtension.php b/src/DI/Extensions/DIExtension.php index 9f0e2ab5d..a51f43d96 100644 --- a/src/DI/Extensions/DIExtension.php +++ b/src/DI/Extensions/DIExtension.php @@ -91,8 +91,6 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class) ) { $this->enableTracyIntegration(); } - - $this->initializeTaggedServices(); } @@ -122,15 +120,6 @@ private function restrictTypes(Nette\PhpGenerator\ClassType $class): void } - private function initializeTaggedServices(): void - { - foreach (array_filter($this->getContainerBuilder()->findByTag('run')) as $name => $on) { - trigger_error("Tag 'run' used in service '$name' definition is deprecated.", E_USER_DEPRECATED); - $this->initialization->addBody('$this->getService(?);', [$name]); - } - } - - private function enableTracyIntegration(): void { Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time; diff --git a/tests/DI/DIExtension.run.phpt b/tests/DI/DIExtension.run.phpt deleted file mode 100644 index a764cb514..000000000 --- a/tests/DI/DIExtension.run.phpt +++ /dev/null @@ -1,32 +0,0 @@ -addExtension('di', new DIExtension); -$loader = new DI\Config\Loader; -$config = $loader->load(Tester\FileMock::create(' -services: - std: {factory: stdClass, tags: [run]} - - {factory: stdClass, tags: [run]} -', 'neon')); - -@eval($compiler->addConfig($config)->setClassName('Container1')->compile()); // @ tag is deprecated - -$container = new Container1; -Assert::false($container->isCreated('std')); - -$container->initialize(); -Assert::true($container->isCreated('std')); diff --git a/tests/DI/Definitions.ServiceDefinition.phpt b/tests/DI/Definitions.ServiceDefinition.phpt index bd9445a90..27a8381d3 100644 --- a/tests/DI/Definitions.ServiceDefinition.phpt +++ b/tests/DI/Definitions.ServiceDefinition.phpt @@ -19,11 +19,6 @@ Assert::exception(function () { $def->setType('Foo'); }, Nette\InvalidArgumentException::class, "Service '': Class or interface 'Foo' not found."); -Assert::exception(function () { - $def = new ServiceDefinition; - $def->setImplement('Foo'); -}, Nette\DeprecatedException::class); - test('', function () { $def = new ServiceDefinition; $def->setType('stdClass'); @@ -35,24 +30,6 @@ test('', function () { Assert::null($def->getEntity()); }); -test('', function () { - $def = new ServiceDefinition; - Assert::error(function () use ($def) { - $def->setClass('stdClass', []); - }, E_USER_DEPRECATED); - Assert::same('stdClass', $def->getType()); - Assert::null($def->getEntity()); -}); - -test('', function () { - $def = new ServiceDefinition; - Assert::error(function () use ($def) { - $def->setClass('stdClass', [1, 2]); - }, E_USER_DEPRECATED); - Assert::same('stdClass', $def->getType()); - Assert::equal(new Statement('stdClass', [1, 2]), $def->getFactory()); -}); - test('', function () { $def = new ServiceDefinition; $def->setFactory('stdClass'); From 4a868d2b781ac270d809b9566b614fb751b27dd8 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 12 Jan 2021 16:23:16 +0100 Subject: [PATCH 07/14] some deprecated methods triggers notice --- src/DI/Definitions/Definition.php | 2 ++ src/DI/Definitions/ImportedDefinition.php | 1 + 2 files changed, 3 insertions(+) diff --git a/src/DI/Definitions/Definition.php b/src/DI/Definitions/Definition.php index f863d3e20..887a75c15 100644 --- a/src/DI/Definitions/Definition.php +++ b/src/DI/Definitions/Definition.php @@ -190,6 +190,7 @@ public function getClass(): ?string /** @deprecated Use '$def instanceof Nette\DI\Definitions\ImportedDefinition' */ public function isDynamic(): bool { + trigger_error(sprintf('Service %s: %s() is deprecated, use "instanceof ImportedDefinition".', $this->getName(), __METHOD__), E_USER_DEPRECATED); return false; } @@ -197,6 +198,7 @@ public function isDynamic(): bool /** @deprecated Use Nette\DI\Definitions\FactoryDefinition or AccessorDefinition */ public function getImplement(): ?string { + trigger_error(sprintf('Service %s: %s() is deprecated.', $this->getName(), __METHOD__), E_USER_DEPRECATED); return null; } diff --git a/src/DI/Definitions/ImportedDefinition.php b/src/DI/Definitions/ImportedDefinition.php index e03c245b7..2002f6f71 100644 --- a/src/DI/Definitions/ImportedDefinition.php +++ b/src/DI/Definitions/ImportedDefinition.php @@ -48,6 +48,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, PhpGenerator $ /** @deprecated use '$def instanceof ImportedDefinition' */ public function isDynamic(): bool { + trigger_error(sprintf('Service %s: %s() is deprecated, use "instanceof ImportedDefinition".', $this->getName(), __METHOD__), E_USER_DEPRECATED); return true; } } From f1cfa0cd9b22bdbeb6c91b4249ef4fc2f6fc9f35 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 17:55:19 +0100 Subject: [PATCH 08/14] coding style --- src/DI/Compiler.php | 4 +- src/DI/Config/Adapters/NeonAdapter.php | 8 +- src/DI/Config/DefinitionSchema.php | 8 +- src/DI/Container.php | 8 +- src/DI/ContainerBuilder.php | 2 +- src/DI/Definitions/ImportedDefinition.php | 2 +- src/DI/Definitions/LocatorDefinition.php | 4 +- src/DI/Definitions/ServiceDefinition.php | 2 +- src/DI/DependencyChecker.php | 2 +- src/DI/Extensions/DIExtension.php | 2 +- src/DI/Extensions/DecoratorExtension.php | 8 +- src/DI/Extensions/InjectExtension.php | 6 +- src/DI/Extensions/SearchExtension.php | 12 +- src/DI/Helpers.php | 10 +- src/DI/Resolver.php | 16 +- tests/DI/Compiler.config.phpt | 6 +- tests/DI/Compiler.dependencies.phpt | 6 +- .../Compiler.dynamicParameters.validator.phpt | 2 +- tests/DI/Compiler.functions.phpt | 2 +- tests/DI/Container.dynamic.phpt | 8 +- tests/DI/Container.static-dynamic.phpt | 10 +- .../DI/ContainerBuilder.getByType.alias.phpt | 6 +- tests/DI/ContainerBuilder.getByType.phpt | 2 +- tests/DI/ContainerBuilder.metadata.phpt | 8 +- tests/DI/ContainerLoader.basic.phpt | 4 +- tests/DI/DecoratorExtension.basic.phpt | 2 +- ...Definitions.AccessorDefinition.render.phpt | 32 +-- .../Definitions.FactoryDefinition.render.phpt | 2 +- tests/DI/Definitions.ImportedDefinition.phpt | 12 +- ...tions.LocatorDefinition.render.create.phpt | 46 ++--- ...initions.LocatorDefinition.render.get.phpt | 46 ++--- ...itions.LocatorDefinition.render.multi.phpt | 40 ++-- tests/DI/Helpers.escape().phpt | 2 +- tests/DI/Helpers.expand().phpt | 2 +- tests/DI/Helpers.filterArguments.phpt | 8 +- tests/DI/NeonAdapter.phpt | 184 +++++++++--------- tests/DI/NeonAdapter.services.phpt | 2 +- tests/DI/PhpAdapter.phpt | 26 +-- tests/DI/Resolver.autowireArguments.phpt | 8 +- 39 files changed, 271 insertions(+), 289 deletions(-) diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index 5a8ceaacf..eb8d7c1b2 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -81,7 +81,7 @@ public function addExtension(?string $name, CompilerExtension $extension) public function getExtensions(string $type = null): array { return $type - ? array_filter($this->extensions, function ($item) use ($type): bool { return $item instanceof $type; }) + ? array_filter($this->extensions, fn($item): bool => $item instanceof $type) : $this->extensions; } @@ -245,7 +245,7 @@ public function processExtensions(): void $hint = Nette\Utils\Helpers::getSuggestion(array_keys($this->extensions), $extra); throw new InvalidConfigurationException( "Found section '$extra' in configuration, but corresponding extension is missing" - . ($hint ? ", did you mean '$hint'?" : '.') + . ($hint ? ", did you mean '$hint'?" : '.'), ); } } diff --git a/src/DI/Config/Adapters/NeonAdapter.php b/src/DI/Config/Adapters/NeonAdapter.php index 43f370724..a98b13adb 100644 --- a/src/DI/Config/Adapters/NeonAdapter.php +++ b/src/DI/Config/Adapters/NeonAdapter.php @@ -57,7 +57,7 @@ public function process(array $arr): array foreach ($this->process($val->attributes) as $st) { $tmp = new Statement( $tmp === null ? $st->getEntity() : [$tmp, ltrim(implode('::', (array) $st->getEntity()), ':')], - $st->arguments + $st->arguments, ); } $val = $tmp; @@ -86,7 +86,7 @@ function (&$val): void { if ($val instanceof Statement) { $val = self::statementToEntity($val); } - } + }, ); return "# generated by Nette\n\n" . Neon\Neon::encode($data, Neon\Neon::BLOCK); } @@ -102,7 +102,7 @@ function (&$val): void { } elseif ($val instanceof Reference) { $val = '@' . $val->getValue(); } - } + }, ); $entity = $val->getEntity(); @@ -115,7 +115,7 @@ function (&$val): void { [ self::statementToEntity($entity[0]), new Neon\Entity('::' . $entity[1], $val->arguments), - ] + ], ); } elseif ($entity[0] instanceof Reference) { $entity = '@' . $entity[0]->getValue() . '::' . $entity[1]; diff --git a/src/DI/Config/DefinitionSchema.php b/src/DI/Config/DefinitionSchema.php index 7d30195fa..1f910d7ab 100644 --- a/src/DI/Config/DefinitionSchema.php +++ b/src/DI/Config/DefinitionSchema.php @@ -93,7 +93,13 @@ public function normalize($def, Context $context) } elseif (is_array($def)) { // back compatibility - if (isset($def['class']) && !isset($def['type']) && !isset($def['factory']) && !isset($def['dynamic']) && !isset($def['imported'])) { + if ( + isset($def['class']) + && !isset($def['type']) + && !isset($def['factory']) + && !isset($def['dynamic']) + && !isset($def['imported']) + ) { $def['factory'] = $def['class']; unset($def['class']); } diff --git a/src/DI/Container.php b/src/DI/Container.php index ef6929765..e8ffe6c5c 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -49,7 +49,7 @@ public function __construct(array $params = []) $this->parameters = $params; $this->methods = array_flip(array_filter( get_class_methods($this), - function ($s) { return preg_match('#^createService.#', $s); } + fn($s) => preg_match('#^createService.#', $s), )); } @@ -329,11 +329,9 @@ public function callMethod(callable $function, array $args = []) private function autowireArguments(\ReflectionFunctionAbstract $function, array $args = []): array { - return Resolver::autowireArguments($function, $args, function (string $type, bool $single) { - return $single + return Resolver::autowireArguments($function, $args, fn(string $type, bool $single) => $single ? $this->getByType($type) - : array_map([$this, 'getService'], $this->findAutowired($type)); - }); + : array_map([$this, 'getService'], $this->findAutowired($type))); } diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 5926a011d..6fd51b5ea 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -388,7 +388,7 @@ public function exportMeta(): array public static function literal(string $code, array $args = null): Nette\PhpGenerator\PhpLiteral { return new Nette\PhpGenerator\PhpLiteral( - $args === null ? $code : Nette\PhpGenerator\Helpers::formatArgs($code, $args) + $args === null ? $code : Nette\PhpGenerator\Helpers::formatArgs($code, $args), ); } diff --git a/src/DI/Definitions/ImportedDefinition.php b/src/DI/Definitions/ImportedDefinition.php index 2002f6f71..d05948fcf 100644 --- a/src/DI/Definitions/ImportedDefinition.php +++ b/src/DI/Definitions/ImportedDefinition.php @@ -40,7 +40,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, PhpGenerator $ $method->setReturnType('void') ->setBody( 'throw new Nette\\DI\\ServiceCreationException(?);', - ["Unable to create imported service '{$this->getName()}', it must be added using addService()"] + ["Unable to create imported service '{$this->getName()}', it must be added using addService()"], ); } diff --git a/src/DI/Definitions/LocatorDefinition.php b/src/DI/Definitions/LocatorDefinition.php index d8a1dcc63..bab7ca7b9 100644 --- a/src/DI/Definitions/LocatorDefinition.php +++ b/src/DI/Definitions/LocatorDefinition.php @@ -45,7 +45,7 @@ public function setImplement(string $type) "Service '%s': Method %s::%s() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.", $this->getName(), $type, - $method->name + $method->name, )); } } @@ -139,7 +139,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe ->setReturnNullable($nullable); if (!$name) { - $class->addProperty('mapping', array_map(function ($item) { return $item->getValue(); }, $this->references)) + $class->addProperty('mapping', array_map(fn($item) => $item->getValue(), $this->references)) ->setPrivate(); $methodInner->setBody('if (!isset($this->mapping[$name])) { diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index 7ea55b04a..d124cadc4 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -186,7 +186,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe $code .= PhpHelpers::formatArgs( "if (!\$service instanceof $type) {\n" . "\tthrow new Nette\\UnexpectedValueException(?);\n}\n", - ["Unable to create service '{$this->getName()}', value returned by factory is not $type type."] + ["Unable to create service '{$this->getName()}', value returned by factory is not $type type."], ); } diff --git a/src/DI/DependencyChecker.php b/src/DI/DependencyChecker.php index 60f075c57..b45b459c3 100644 --- a/src/DI/DependencyChecker.php +++ b/src/DI/DependencyChecker.php @@ -86,7 +86,7 @@ public static function isExpired( array &$phpFiles, array $classes, array $functions, - string $hash + string $hash, ): bool { try { $currentFiles = @array_map('filemtime', array_combine($tmp = array_keys($files), $tmp)); // @ - files may not exist diff --git a/src/DI/Extensions/DIExtension.php b/src/DI/Extensions/DIExtension.php index a51f43d96..1b94dedd9 100644 --- a/src/DI/Extensions/DIExtension.php +++ b/src/DI/Extensions/DIExtension.php @@ -115,7 +115,7 @@ private function restrictTypes(Nette\PhpGenerator\ClassType $class): void $prop = $class->getProperty('wiring'); $prop->value = array_intersect_key( $prop->value, - $this->exportedTypes + (is_array($option) ? array_flip($option) : []) + $this->exportedTypes + (is_array($option) ? array_flip($option) : []), ); } diff --git a/src/DI/Extensions/DecoratorExtension.php b/src/DI/Extensions/DecoratorExtension.php index 91efc27cb..beb61879a 100644 --- a/src/DI/Extensions/DecoratorExtension.php +++ b/src/DI/Extensions/DecoratorExtension.php @@ -25,7 +25,7 @@ public function getConfigSchema(): Nette\Schema\Schema 'setup' => Expect::list(), 'tags' => Expect::array(), 'inject' => Expect::bool(), - ]) + ]), ); } @@ -73,9 +73,7 @@ public function addTags(string $type, array $tags): void private function findByType(string $type): array { - return array_filter($this->getContainerBuilder()->getDefinitions(), function (Definitions\Definition $def) use ($type): bool { - return is_a($def->getType(), $type, true) - || ($def instanceof Definitions\FactoryDefinition && is_a($def->getResultType(), $type, true)); - }); + return array_filter($this->getContainerBuilder()->getDefinitions(), fn(Definitions\Definition $def): bool => is_a($def->getType(), $type, true) + || ($def instanceof Definitions\FactoryDefinition && is_a($def->getResultType(), $type, true))); } } diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index f5a890a58..976988e8c 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -94,11 +94,9 @@ public static function getInjectMethods(string $class): array } } $methods = array_keys($classes); - uksort($classes, function (string $a, string $b) use ($classes, $methods): int { - return $classes[$a] === $classes[$b] + uksort($classes, fn(string $a, string $b): int => $classes[$a] === $classes[$b] ? array_search($a, $methods, true) <=> array_search($b, $methods, true) - : (is_a($classes[$a], $classes[$b], true) ? 1 : -1); - }); + : (is_a($classes[$a], $classes[$b], true) ? 1 : -1)); return array_keys($classes); } diff --git a/src/DI/Extensions/SearchExtension.php b/src/DI/Extensions/SearchExtension.php index 5c1d0a41f..0b375e17d 100644 --- a/src/DI/Extensions/SearchExtension.php +++ b/src/DI/Extensions/SearchExtension.php @@ -48,12 +48,10 @@ public function getConfigSchema(): Nette\Schema\Schema 'implements' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]), ]), 'tags' => Expect::array(), - ]) - )->before(function ($val) { - return is_string($val['in'] ?? null) + ]), + )->before(fn($val) => is_string($val['in'] ?? null) ? ['default' => $val] - : $val; - }); + : $val); } @@ -102,8 +100,8 @@ public function findClasses(\stdClass $config): array ) && (!$acceptRE || preg_match($acceptRE, $rc->name)) && (!$rejectRE || !preg_match($rejectRE, $rc->name)) - && (!$acceptParent || Arrays::some($acceptParent, function ($nm) use ($rc) { return $rc->isSubclassOf($nm); })) - && (!$rejectParent || Arrays::every($rejectParent, function ($nm) use ($rc) { return !$rc->isSubclassOf($nm); })) + && (!$acceptParent || Arrays::some($acceptParent, fn($nm) => $rc->isSubclassOf($nm))) + && (!$rejectParent || Arrays::every($rejectParent, fn($nm) => !$rc->isSubclassOf($nm))) ) { $found[] = $rc->name; } diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index a164f1d26..690d1b1c7 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -90,12 +90,10 @@ public static function expand($var, array $params, $recursive = false) } } if ($php) { - $res = array_filter($res, function ($val): bool { return $val !== ''; }); - $res = array_map(function ($val): string { - return $val instanceof DynamicParameter + $res = array_filter($res, fn($val): bool => $val !== ''); + $res = array_map(fn($val): string => $val instanceof DynamicParameter ? "($val)" - : var_export((string) $val, true); - }, $res); + : var_export((string) $val, true), $res); return new DynamicParameter(implode(' . ', $res)); } return implode('', $res); @@ -164,7 +162,7 @@ public static function prefixServiceName($config, string $namespace) } elseif ($config instanceof Statement) { return new Statement( self::prefixServiceName($config->getEntity(), $namespace), - self::prefixServiceName($config->arguments, $namespace) + self::prefixServiceName($config->arguments, $namespace), ); } elseif (is_array($config)) { foreach ($config as &$val) { diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index f63100dac..085a79b17 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -60,7 +60,7 @@ public function getContainerBuilder(): ContainerBuilder public function resolveDefinition(Definition $def): void { if ($this->recursive->contains($def)) { - $names = array_map(function ($item) { return $item->getName(); }, iterator_to_array($this->recursive)); + $names = array_map(fn($item) => $item->getName(), iterator_to_array($this->recursive)); throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', $names))); } @@ -139,7 +139,7 @@ public function resolveEntityType(Statement $statement): ?string throw new ServiceCreationException( interface_exists($entity) ? "Interface $entity can not be used as 'factory', did you mean 'implement'?" - : "Class $entity not found." + : "Class $entity not found.", ); } return $entity; @@ -175,11 +175,9 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo $this->currentServiceAllowed = $currentServiceAllowed; $entity = $this->normalizeEntity($statement); $arguments = $this->convertReferences($statement->arguments); - $getter = function (string $type, bool $single) { - return $single + $getter = fn(string $type, bool $single) => $single ? $this->getByType($type) - : array_values(array_filter($this->builder->findAutowired($type), function ($obj) { return $obj !== $this->currentService; })); - }; + : array_values(array_filter($this->builder->findAutowired($type), fn($obj) => $obj !== $this->currentService)); switch (true) { case is_string($entity) && Strings::contains($entity, '?'): // PHP literal @@ -439,11 +437,9 @@ private function completeException(\Exception $e, Definition $def): ServiceCreat private function entityToString($entity): string { - $referenceToText = function (Reference $ref): string { - return $ref->isSelf() && $this->currentService + $referenceToText = fn(Reference $ref): string => $ref->isSelf() && $this->currentService ? '@' . $this->currentService->getName() : '@' . $ref->getValue(); - }; if (is_string($entity)) { return $entity . '::__construct()'; } elseif ($entity instanceof Reference) { @@ -492,7 +488,7 @@ private function convertReferences(array $arguments): array public static function autowireArguments( \ReflectionFunctionAbstract $method, array $arguments, - callable $getter + callable $getter, ): array { $optCount = 0; $num = -1; diff --git a/tests/DI/Compiler.config.phpt b/tests/DI/Compiler.config.phpt index bcd1a7b03..26f804d62 100644 --- a/tests/DI/Compiler.config.phpt +++ b/tests/DI/Compiler.config.phpt @@ -17,7 +17,7 @@ test('Compiler config', function () { Assert::same( [], - $compiler->getConfig() + $compiler->getConfig(), ); $compiler->addConfig([ @@ -34,7 +34,7 @@ test('Compiler config', function () { ], 'services' => [], ], - $compiler->getConfig() + $compiler->getConfig(), ); }); @@ -55,6 +55,6 @@ parameters: Assert::same( ['php', 'node'], - $compiler->getConfig()['parameters']['languages'] + $compiler->getConfig()['parameters']['languages'], ); }); diff --git a/tests/DI/Compiler.dependencies.phpt b/tests/DI/Compiler.dependencies.phpt index 68af9dd44..b24923cf8 100644 --- a/tests/DI/Compiler.dependencies.phpt +++ b/tests/DI/Compiler.dependencies.phpt @@ -17,7 +17,7 @@ $compiler = new DI\Compiler; Assert::same( [DependencyChecker::VERSION, [], [], [], [], '40cd750bba9870f18aada2478b24840a'], - $compiler->exportDependencies() + $compiler->exportDependencies(), ); Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies())); @@ -25,7 +25,7 @@ Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies())); $compiler->addDependencies(['file1', __FILE__]); Assert::same( [DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__)], [], [], [], '40cd750bba9870f18aada2478b24840a'], - $compiler->exportDependencies() + $compiler->exportDependencies(), ); Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies())); @@ -33,7 +33,7 @@ Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies())); $compiler->addDependencies(['file1', null, 'file3']); Assert::same( [DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__), 'file3' => false], [], [], [], '40cd750bba9870f18aada2478b24840a'], - $compiler->exportDependencies() + $compiler->exportDependencies(), ); $res = $compiler->exportDependencies(); diff --git a/tests/DI/Compiler.dynamicParameters.validator.phpt b/tests/DI/Compiler.dynamicParameters.validator.phpt index 0d6cb4eb7..efd4f208b 100644 --- a/tests/DI/Compiler.dynamicParameters.validator.phpt +++ b/tests/DI/Compiler.dynamicParameters.validator.phpt @@ -18,7 +18,7 @@ class FooExtension extends Nette\DI\CompilerExtension Expect::structure([ 'string' => Expect::string()->dynamic(), 'intnull' => Expect::int()->nullable()->dynamic(), - ]) + ]), ); } } diff --git a/tests/DI/Compiler.functions.phpt b/tests/DI/Compiler.functions.phpt index 02b2fccb6..bf027462e 100644 --- a/tests/DI/Compiler.functions.phpt +++ b/tests/DI/Compiler.functions.phpt @@ -67,7 +67,7 @@ Assert::same( 'int' => [0, 1, 231, 123], 'float' => [0.0, 1.0, 231.0, 123.0], ], - $obj->args + $obj->args, ); Assert::exception(function () use ($container) { diff --git a/tests/DI/Container.dynamic.phpt b/tests/DI/Container.dynamic.phpt index 827c5d475..2f71ed403 100644 --- a/tests/DI/Container.dynamic.phpt +++ b/tests/DI/Container.dynamic.phpt @@ -40,9 +40,7 @@ test('', function () use ($container) { test('closure', function () use ($container) { - @$container->addService('four', function () { // @ triggers service should be defined as "imported" - return new Service; - }); + @$container->addService('four', fn() => new Service); Assert::true($container->hasService('four')); Assert::false($container->isCreated('four')); @@ -55,9 +53,7 @@ test('closure', function () use ($container) { test('closure with typehint', function () use ($container) { - @$container->addService('five', function (): Service { // @ triggers service should be defined as "imported" - return new Service; - }); + @$container->addService('five', fn(): Service => new Service); Assert::same(Service::class, $container->getServiceType('five')); }); diff --git a/tests/DI/Container.static-dynamic.phpt b/tests/DI/Container.static-dynamic.phpt index 7d9e2a40d..1fde5b2c3 100644 --- a/tests/DI/Container.static-dynamic.phpt +++ b/tests/DI/Container.static-dynamic.phpt @@ -46,7 +46,7 @@ test('', function () { test('closure', function () { $container = new MyContainer; - $container->addService('one', function () { return new stdClass; }); + $container->addService('one', fn() => new stdClass); Assert::true($container->hasService('one')); Assert::same('', $container->getServiceType('one')); @@ -58,7 +58,7 @@ test('closure', function () { test('closure & typehint', function () { $container = new MyContainer; - $container->addService('one', function (): stdClass { return new stdClass; }); + $container->addService('one', fn(): stdClass => new stdClass); Assert::same(stdClass::class, $container->getServiceType('one')); Assert::true($container->hasService('one')); @@ -73,7 +73,7 @@ test('closure & matching typehint', function () { { } - $container->addService('typehint', function (): MyClass { return new MyClass; }); + $container->addService('typehint', fn(): MyClass => new MyClass); Assert::same(MyClass::class, $container->getServiceType('typehint')); Assert::true($container->hasService('typehint')); @@ -83,11 +83,11 @@ test('closure & matching typehint', function () { Assert::exception(function () { // closure & wrong typehint $container = new MyContainer; - $container->addService('typehint', function () { return new DateTime; }); + $container->addService('typehint', fn() => new DateTime); }, Nette\InvalidArgumentException::class, "Service 'typehint' must be instance of stdClass, add typehint to closure."); Assert::exception(function () { // closure & wrong typehint $container = new MyContainer; - $container->addService('typehint', function (): DateTime { return new DateTime; }); + $container->addService('typehint', fn(): DateTime => new DateTime); }, Nette\InvalidArgumentException::class, "Service 'typehint' must be instance of stdClass, DateTime given."); diff --git a/tests/DI/ContainerBuilder.getByType.alias.phpt b/tests/DI/ContainerBuilder.getByType.alias.phpt index 7f47bc7d2..22ed1a412 100644 --- a/tests/DI/ContainerBuilder.getByType.alias.phpt +++ b/tests/DI/ContainerBuilder.getByType.alias.phpt @@ -39,15 +39,15 @@ Assert::exception(function () use ($builder) { Assert::same( ['one' => $builder->getDefinition('one'), 'two' => $builder->getDefinition('two')], - $builder->findByType('Service') + $builder->findByType('Service'), ); Assert::same( ['one' => $builder->getDefinition('one'), 'two' => $builder->getDefinition('two')], - $builder->findByType('Alias') + $builder->findByType('Alias'), ); Assert::same( ['one' => $builder->getDefinition('one'), 'two' => $builder->getDefinition('two')], - $builder->findByType('\service') + $builder->findByType('\service'), ); diff --git a/tests/DI/ContainerBuilder.getByType.phpt b/tests/DI/ContainerBuilder.getByType.phpt index 0ddd40b82..66c842059 100644 --- a/tests/DI/ContainerBuilder.getByType.phpt +++ b/tests/DI/ContainerBuilder.getByType.phpt @@ -73,6 +73,6 @@ Assert::same([ Assert::same( ['two' => $builder->getDefinition('two'), 'three' => $builder->getDefinition('three')], - $builder->findByType('Service2') + $builder->findByType('Service2'), ); Assert::same([], $builder->findByType('unknown')); diff --git a/tests/DI/ContainerBuilder.metadata.phpt b/tests/DI/ContainerBuilder.metadata.phpt index 508846464..010331fe8 100644 --- a/tests/DI/ContainerBuilder.metadata.phpt +++ b/tests/DI/ContainerBuilder.metadata.phpt @@ -38,12 +38,12 @@ Assert::same( Nette\DI\Container::class => [['container']], stdClass::class => [['lorem']], ], - getPropertyValue($container, 'wiring') + getPropertyValue($container, 'wiring'), ); Assert::same( ['container' => Nette\DI\Container::class], - getPropertyValue($container, 'types') + getPropertyValue($container, 'types'), ); Assert::same( @@ -52,12 +52,12 @@ Assert::same( 'b' => ['lorem' => 'c'], 'd' => ['lorem' => ['e']], ], - getPropertyValue($container, 'tags') + getPropertyValue($container, 'tags'), ); Assert::same( [], - getPropertyValue($container, 'aliases') + getPropertyValue($container, 'aliases'), ); Assert::same(['lorem' => true], $container->findByTag('a')); diff --git a/tests/DI/ContainerLoader.basic.phpt b/tests/DI/ContainerLoader.basic.phpt index 7b1a92aa1..9f75a0b5f 100644 --- a/tests/DI/ContainerLoader.basic.phpt +++ b/tests/DI/ContainerLoader.basic.phpt @@ -19,9 +19,7 @@ $key = [1, 2]; $className = $cache->getClassName($key); Assert::match('Container%[\w]+%', $className); -$container = $cache->load(function () use ($className) { - return "class $className {}"; -}, $key); +$container = $cache->load(fn() => "class $className {}", $key); Assert::type($className, new $container); $container = $cache->load(function () {}, 'key2'); diff --git a/tests/DI/DecoratorExtension.basic.phpt b/tests/DI/DecoratorExtension.basic.phpt index 8747145a9..08667d50e 100644 --- a/tests/DI/DecoratorExtension.basic.phpt +++ b/tests/DI/DecoratorExtension.basic.phpt @@ -78,7 +78,7 @@ $builder = $compiler->getContainerBuilder(); Assert::same( ['a' => true, 'tag' => 2, DI\Extensions\InjectExtension::TAG_INJECT => true, 'Iface' => true], - $builder->getDefinition('one')->getTags() + $builder->getDefinition('one')->getTags(), ); Assert::true($builder->getDefinition('one')->getTag(DI\Extensions\InjectExtension::TAG_INJECT)); diff --git a/tests/DI/Definitions.AccessorDefinition.render.phpt b/tests/DI/Definitions.AccessorDefinition.render.phpt index 5c062efc7..6bf5012d2 100644 --- a/tests/DI/Definitions.AccessorDefinition.render.phpt +++ b/tests/DI/Definitions.AccessorDefinition.render.phpt @@ -36,26 +36,26 @@ test('', function () { Assert::match( <<<'XX' -public function createServiceAbc(): Good2 -{ - return new class ($this) implements Good2 { - private $container; + public function createServiceAbc(): Good2 + { + return new class ($this) implements Good2 { + private $container; - public function __construct($container) - { - $this->container = $container; - } + public function __construct($container) + { + $this->container = $container; + } - public function get(): stdClass - { - return $this->container->getService('a'); - } - }; -} -XX + public function get(): stdClass + { + return $this->container->getService('a'); + } + }; + } + XX , - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Definitions.FactoryDefinition.render.phpt b/tests/DI/Definitions.FactoryDefinition.render.phpt index 4aeefd7b4..4f2fb809a 100644 --- a/tests/DI/Definitions.FactoryDefinition.render.phpt +++ b/tests/DI/Definitions.FactoryDefinition.render.phpt @@ -53,6 +53,6 @@ test('', function () { } }; }', - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Definitions.ImportedDefinition.phpt b/tests/DI/Definitions.ImportedDefinition.phpt index 1e3c2d8a9..ee1ac5502 100644 --- a/tests/DI/Definitions.ImportedDefinition.phpt +++ b/tests/DI/Definitions.ImportedDefinition.phpt @@ -42,12 +42,12 @@ test('', function () { Assert::match( <<<'XX' -public function createServiceAbc(): void -{ - throw new Nette\DI\ServiceCreationException('Unable to create imported service \'abc\', it must be added using addService()'); -} -XX + public function createServiceAbc(): void + { + throw new Nette\DI\ServiceCreationException('Unable to create imported service \'abc\', it must be added using addService()'); + } + XX , - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Definitions.LocatorDefinition.render.create.phpt b/tests/DI/Definitions.LocatorDefinition.render.create.phpt index 22bbd5899..fe0292ef7 100644 --- a/tests/DI/Definitions.LocatorDefinition.render.create.phpt +++ b/tests/DI/Definitions.LocatorDefinition.render.create.phpt @@ -37,30 +37,30 @@ test('', function () { Assert::match( <<<'XX' -public function createServiceAbc(): Good -{ - return new class ($this) implements Good { - private $container;%A?% - private $mapping = ['first' => 'a', 'second' => 'a']; - - - public function __construct($container) - { - $this->container = $container; - } - - - public function create($name): stdClass - { - if (!isset($this->mapping[$name])) { - throw new Nette\DI\MissingServiceException("Service '$name' is not defined."); + public function createServiceAbc(): Good + { + return new class ($this) implements Good { + private $container;%A?% + private $mapping = ['first' => 'a', 'second' => 'a']; + + + public function __construct($container) + { + $this->container = $container; + } + + + public function create($name): stdClass + { + if (!isset($this->mapping[$name])) { + throw new Nette\DI\MissingServiceException("Service '$name' is not defined."); + } + return $this->container->createService($this->mapping[$name]); + } + }; } - return $this->container->createService($this->mapping[$name]); - } - }; -} -XX + XX , - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Definitions.LocatorDefinition.render.get.phpt b/tests/DI/Definitions.LocatorDefinition.render.get.phpt index a0f399d12..d848b804c 100644 --- a/tests/DI/Definitions.LocatorDefinition.render.get.phpt +++ b/tests/DI/Definitions.LocatorDefinition.render.get.phpt @@ -37,30 +37,30 @@ test('', function () { Assert::match( <<<'XX' -public function createServiceAbc(): Good -{ - return new class ($this) implements Good { - private $container;%A?% - private $mapping = ['first' => 'a', 'second' => 'a']; - - - public function __construct($container) - { - $this->container = $container; - } - - - public function get($name): stdClass - { - if (!isset($this->mapping[$name])) { - throw new Nette\DI\MissingServiceException("Service '$name' is not defined."); + public function createServiceAbc(): Good + { + return new class ($this) implements Good { + private $container;%A?% + private $mapping = ['first' => 'a', 'second' => 'a']; + + + public function __construct($container) + { + $this->container = $container; + } + + + public function get($name): stdClass + { + if (!isset($this->mapping[$name])) { + throw new Nette\DI\MissingServiceException("Service '$name' is not defined."); + } + return $this->container->getService($this->mapping[$name]); + } + }; } - return $this->container->getService($this->mapping[$name]); - } - }; -} -XX + XX , - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Definitions.LocatorDefinition.render.multi.phpt b/tests/DI/Definitions.LocatorDefinition.render.multi.phpt index 5f7942fb0..4b077c50e 100644 --- a/tests/DI/Definitions.LocatorDefinition.render.multi.phpt +++ b/tests/DI/Definitions.LocatorDefinition.render.multi.phpt @@ -39,32 +39,32 @@ test('', function () { Assert::match( <<<'XX' -public function createServiceAbc(): Good -{ - return new class ($this) implements Good { - private $container; + public function createServiceAbc(): Good + { + return new class ($this) implements Good { + private $container; - public function __construct($container) - { - $this->container = $container; - } + public function __construct($container) + { + $this->container = $container; + } - public function createFirst(): stdClass - { - return $this->container->createServiceA(); - } + public function createFirst(): stdClass + { + return $this->container->createServiceA(); + } - public function getSecond(): ?stdClass - { - return $this->container->getService('a'); - } - }; -} -XX + public function getSecond(): ?stdClass + { + return $this->container->getService('a'); + } + }; + } + XX , - $method->__toString() + $method->__toString(), ); }); diff --git a/tests/DI/Helpers.escape().phpt b/tests/DI/Helpers.escape().phpt index 522946581..86a92be96 100644 --- a/tests/DI/Helpers.escape().phpt +++ b/tests/DI/Helpers.escape().phpt @@ -19,5 +19,5 @@ Assert::same('@@', Helpers::escape('@')); Assert::same('x@', Helpers::escape('x@')); Assert::same( ['key1' => '%%', 'key2' => '@@', '%%a%%' => 123, '@' => 123], - Helpers::escape(['key1' => '%', 'key2' => '@', '%a%' => 123, '@' => 123]) + Helpers::escape(['key1' => '%', 'key2' => '@', '%a%' => 123, '@' => 123]), ); diff --git a/tests/DI/Helpers.expand().phpt b/tests/DI/Helpers.expand().phpt index a515d5719..90aef87cf 100644 --- a/tests/DI/Helpers.expand().phpt +++ b/tests/DI/Helpers.expand().phpt @@ -30,7 +30,7 @@ Assert::same( Helpers::expand('%keyA%', [ 'keyA' => ['key1' => 123, 'key2' => '%keyB%'], 'keyB' => 'abc', - ], true) + ], true), ); Assert::equal(new PhpLiteral('func()'), Helpers::expand('%key%', ['key' => new PhpLiteral('func()')])); diff --git a/tests/DI/Helpers.filterArguments.phpt b/tests/DI/Helpers.filterArguments.phpt index 35cdf7a39..0ec8d5fa0 100644 --- a/tests/DI/Helpers.filterArguments.phpt +++ b/tests/DI/Helpers.filterArguments.phpt @@ -18,20 +18,20 @@ Assert::same([], Helpers::filterArguments([])); Assert::same( ['a', 'b', 3 => ['c'], [1 => 'd']], - Helpers::filterArguments(['a', 'b', '...', ['c', '...'], ['...', 'd']]) + Helpers::filterArguments(['a', 'b', '...', ['c', '...'], ['...', 'd']]), ); Assert::same( ['a', 'b', Nette\DI\ContainerBuilder::THIS_CONTAINER], - Helpers::filterArguments(['a', 'b', 'Nette\DI\ContainerBuilder::THIS_CONTAINER']) + Helpers::filterArguments(['a', 'b', 'Nette\DI\ContainerBuilder::THIS_CONTAINER']), ); Assert::equal( ['a', 'b', new Nette\DI\Definitions\Reference('service')], - Helpers::filterArguments(['a', 'b', '@service']) + Helpers::filterArguments(['a', 'b', '@service']), ); Assert::equal( [new Statement('class', ['a', 2 => Nette\DI\ContainerBuilder::THIS_CONTAINER])], - Helpers::filterArguments([new Statement('class', ['a', '...', 'Nette\DI\ContainerBuilder::THIS_CONTAINER'])]) + Helpers::filterArguments([new Statement('class', ['a', '...', 'Nette\DI\ContainerBuilder::THIS_CONTAINER'])]), ); diff --git a/tests/DI/NeonAdapter.phpt b/tests/DI/NeonAdapter.phpt index af480b8fe..0fc39e45d 100644 --- a/tests/DI/NeonAdapter.phpt +++ b/tests/DI/NeonAdapter.phpt @@ -44,32 +44,32 @@ Assert::same([ $config->save($data, TEMP_FILE); Assert::match(<<<'EOD' -# generated by Nette - -section: - webname: the example - database: - adapter: pdo_mysql - params: - host: db.example.com - username: dbuser - password: secret - dbname: dbname - - timeout: 10 - display_errors: true - html_errors: false - items: - - 10 - - 20 - - php: - zlib.output_compression: true - date.timezone: Europe/Prague - -override: [] -nothing: null -EOD + # generated by Nette + + section: + webname: the example + database: + adapter: pdo_mysql + params: + host: db.example.com + username: dbuser + password: secret + dbname: dbname + + timeout: 10 + display_errors: true + html_errors: false + items: + - 10 + - 20 + + php: + zlib.output_compression: true + date.timezone: Europe/Prague + + override: [] + nothing: null + EOD , file_get_contents(TEMP_FILE)); @@ -86,87 +86,87 @@ Assert::equal([ new Statement('ent', [2]), 'inner', ], - [3, 4] + [3, 4], ), new Statement( [ new Statement('ent', [3]), 'inner', ], - [5] + [5], ), ], $data); $config->save($data, TEMP_FILE); Assert::match(<<<'EOD' -# generated by Nette + # generated by Nette -- ent(1) -- ent(2)::inner(3, 4) -- ent(3)::inner(5) -EOD + - ent(1) + - ent(2)::inner(3, 4) + - ent(3)::inner(5) + EOD , file_get_contents(TEMP_FILE)); $data = $config->load('files/neonAdapter.save.neon'); $config->save($data, TEMP_FILE); Assert::match(<<<'EOD' -# generated by Nette - -parameters: - type: Ipsum - -services: - referencedService: @one - referencedServiceWithSetup: - factory: @one - setup: - - $x(10) - - serviceAsParam: Ipsum(@one) - calledService: @one() - calledServiceWithArgs: @one(1) - calledServiceAsParam: Ipsum(@one()) - calledServiceWithArgsAsParam: Ipsum(@one(1)) - one: - type: %class% - arguments: - - 1 - - two: - factory: %class%(1) - - three: - type: Lorem - factory: Factory::createLorem - arguments: - - 1 - - four: - factory: Factory::createLorem(1) - - five: - factory: Factory::createLorem(1) - - six: Factory::createLorem(1) - seven: @factory - eight: @factory() - nine: - - @three - - foo - - stdClass: stdClass - factory: Lorem - rich1: Lorem(1)::foo() - rich2: - factory: Lorem(Ipsum(@one))::foo(1) - - rich3: Factory::createLorem(1)::foo() - rich4: Factory()::createLorem(1)::foo() - 0: Lorem(1)::foo() - -EOD + # generated by Nette + + parameters: + type: Ipsum + + services: + referencedService: @one + referencedServiceWithSetup: + factory: @one + setup: + - $x(10) + + serviceAsParam: Ipsum(@one) + calledService: @one() + calledServiceWithArgs: @one(1) + calledServiceAsParam: Ipsum(@one()) + calledServiceWithArgsAsParam: Ipsum(@one(1)) + one: + type: %class% + arguments: + - 1 + + two: + factory: %class%(1) + + three: + type: Lorem + factory: Factory::createLorem + arguments: + - 1 + + four: + factory: Factory::createLorem(1) + + five: + factory: Factory::createLorem(1) + + six: Factory::createLorem(1) + seven: @factory + eight: @factory() + nine: + - @three + - foo + + stdClass: stdClass + factory: Lorem + rich1: Lorem(1)::foo() + rich2: + factory: Lorem(Ipsum(@one))::foo(1) + + rich3: Factory::createLorem(1)::foo() + rich4: Factory()::createLorem(1)::foo() + 0: Lorem(1)::foo() + + EOD , file_get_contents(TEMP_FILE)); @@ -176,9 +176,9 @@ $data = [ ]; $config->save($data, TEMP_FILE); Assert::match(<<<'EOD' -# generated by Nette + # generated by Nette -a: @foo::method(@bar) -b: @foo() -EOD + a: @foo::method(@bar) + b: @foo() + EOD , file_get_contents(TEMP_FILE)); diff --git a/tests/DI/NeonAdapter.services.phpt b/tests/DI/NeonAdapter.services.phpt index eea45f61d..7b4e861d8 100644 --- a/tests/DI/NeonAdapter.services.phpt +++ b/tests/DI/NeonAdapter.services.phpt @@ -26,5 +26,5 @@ Assert::equal( new Statement('Class2', ['arg2', 'arg3']), ]), ], - $data + $data, ); diff --git a/tests/DI/PhpAdapter.phpt b/tests/DI/PhpAdapter.phpt index ce0283335..730a4ac59 100644 --- a/tests/DI/PhpAdapter.phpt +++ b/tests/DI/PhpAdapter.phpt @@ -34,18 +34,18 @@ Assert::same([ $config->save($data, TEMP_FILE); Assert::match(<<<'EOD' - 'the example', - 'database' => [ - 'adapter' => 'pdo_mysql', - 'params' => [ - 'host' => 'db.example.com', - 'username' => 'dbuser', - 'password' => '*secret*', - 'dbname' => 'dbname', + 'the example', + 'database' => [ + 'adapter' => 'pdo_mysql', + 'params' => [ + 'host' => 'db.example.com', + 'username' => 'dbuser', + 'password' => '*secret*', + 'dbname' => 'dbname', + ], ], - ], -]; -EOD + ]; + EOD , file_get_contents(TEMP_FILE)); diff --git a/tests/DI/Resolver.autowireArguments.phpt b/tests/DI/Resolver.autowireArguments.phpt index aeae56194..ad22418c3 100644 --- a/tests/DI/Resolver.autowireArguments.phpt +++ b/tests/DI/Resolver.autowireArguments.phpt @@ -27,14 +27,10 @@ class Test Assert::equal( [new Test, new Test], - Resolver::autowireArguments(new ReflectionMethod('Test', 'method'), [], function ($type) { - return $type === 'Test' ? new Test : null; - }) + Resolver::autowireArguments(new ReflectionMethod('Test', 'method'), [], fn($type) => $type === 'Test' ? new Test : null), ); Assert::equal( [new Test, new Test, null, null], - Resolver::autowireArguments(new ReflectionMethod('Test', 'methodNullable'), [], function ($type) { - return $type === 'Test' ? new Test : null; - }) + Resolver::autowireArguments(new ReflectionMethod('Test', 'methodNullable'), [], fn($type) => $type === 'Test' ? new Test : null), ); From 9ec97d8481e9188646208b678a8d22f8527c59dd Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 2 Mar 2021 04:43:59 +0100 Subject: [PATCH 09/14] removed support for PHP 7 --- src/DI/Definitions/FactoryDefinition.php | 5 +-- src/DI/DependencyChecker.php | 2 +- src/DI/Extensions/InjectExtension.php | 2 +- tests/DI/Resolver.autowireArguments.80.phpt | 47 --------------------- tests/DI/Resolver.autowireArguments.phpt | 29 +++++++++++++ 5 files changed, 32 insertions(+), 53 deletions(-) delete mode 100644 tests/DI/Resolver.autowireArguments.80.phpt diff --git a/src/DI/Definitions/FactoryDefinition.php b/src/DI/Definitions/FactoryDefinition.php index 3303163c8..4aa2ee76c 100644 --- a/src/DI/Definitions/FactoryDefinition.php +++ b/src/DI/Definitions/FactoryDefinition.php @@ -171,10 +171,7 @@ private function completeParameters(Nette\DI\Resolver $resolver): void throw new ServiceCreationException("Unused parameter \${$param->name} when implementing method $interface::create()" . ($hint ? ", did you mean \${$hint}?" : '.')); } - $paramDef = PHP_VERSION_ID < 80000 - ? ($methodHint && $param->allowsNull() ? '?' : '') . reset($methodHint) - : implode('|', $methodHint); - $paramDef .= ' ' . $param->name; + $paramDef = implode('|', $methodHint) . ' ' . $param->name; if ($param->isDefaultValueAvailable()) { $this->parameters[$paramDef] = Reflection::getParameterDefaultValue($param); } else { diff --git a/src/DI/DependencyChecker.php b/src/DI/DependencyChecker.php index b45b459c3..b62db69e7 100644 --- a/src/DI/DependencyChecker.php +++ b/src/DI/DependencyChecker.php @@ -122,7 +122,7 @@ class_uses($name), $prop->name, $prop->getDocComment(), Reflection::getPropertyTypes($prop), - PHP_VERSION_ID >= 80000 ? count($prop->getAttributes(Attributes\Inject::class)) : null, + count($prop->getAttributes(Attributes\Inject::class)), ]; } } diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index 976988e8c..098107f1f 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -110,7 +110,7 @@ public static function getInjectProperties(string $class): array $res = []; foreach (get_class_vars($class) as $name => $foo) { $rp = new \ReflectionProperty($class, $name); - $hasAttr = PHP_VERSION_ID >= 80000 && $rp->getAttributes(DI\Attributes\Inject::class); + $hasAttr = $rp->getAttributes(DI\Attributes\Inject::class); if ($hasAttr || DI\Helpers::parseAnnotation($rp, 'inject') !== null) { if ($type = Reflection::getPropertyType($rp)) { } elseif (!$hasAttr && ($type = DI\Helpers::parseAnnotation($rp, 'var'))) { diff --git a/tests/DI/Resolver.autowireArguments.80.phpt b/tests/DI/Resolver.autowireArguments.80.phpt deleted file mode 100644 index ed9bf63f1..000000000 --- a/tests/DI/Resolver.autowireArguments.80.phpt +++ /dev/null @@ -1,47 +0,0 @@ - $type === 'Test' ? new Test : null), ); + +Assert::exception(function () { + Resolver::autowireArguments(new ReflectionMethod('Test', 'methodUnion'), [], function () {}); +}, Nette\InvalidStateException::class, 'Parameter $self in Test::methodUnion() has union type hint and no default value, so its value must be specified.'); + +Assert::same( + [null], + Resolver::autowireArguments(new ReflectionMethod('Test', 'methodUnionNullable'), [], function () {}), +); + +Assert::same( + [], + Resolver::autowireArguments(new ReflectionMethod('Test', 'methodUnionDefault'), [], function () {}), +); From cfebef14b91b0164c60443274bd51d09176ee5c7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 19:11:39 +0100 Subject: [PATCH 10/14] added property typehints --- src/Bridges/DITracy/ContainerPanel.php | 9 +++---- src/DI/Autowiring.php | 9 +++---- src/DI/Compiler.php | 19 +++++--------- src/DI/CompilerExtension.php | 12 +++------ src/DI/Config/DefinitionSchema.php | 3 +-- src/DI/Config/Loader.php | 8 +++--- src/DI/Container.php | 7 +++-- src/DI/ContainerBuilder.php | 21 ++++++--------- src/DI/ContainerLoader.php | 6 ++--- src/DI/Definitions/AccessorDefinition.php | 3 +-- src/DI/Definitions/Definition.php | 14 +++++----- src/DI/Definitions/FactoryDefinition.php | 6 ++--- src/DI/Definitions/LocatorDefinition.php | 5 ++-- src/DI/Definitions/Reference.php | 3 +-- src/DI/Definitions/ServiceDefinition.php | 5 ++-- src/DI/Definitions/Statement.php | 6 ++--- src/DI/DependencyChecker.php | 4 +-- src/DI/Extensions/DIExtension.php | 26 +++++++------------ src/DI/Extensions/ParametersExtension.php | 5 ++-- src/DI/Extensions/SearchExtension.php | 6 ++--- src/DI/PhpGenerator.php | 6 ++--- src/DI/Resolver.php | 16 +++++------- tests/DI/Compiler.arguments.phpt | 2 +- .../DI/CompilerExtension.validateConfig.phpt | 2 +- tests/bootstrap.php | 2 +- 25 files changed, 78 insertions(+), 127 deletions(-) diff --git a/src/Bridges/DITracy/ContainerPanel.php b/src/Bridges/DITracy/ContainerPanel.php index 3fff3ff9f..124a1fc01 100644 --- a/src/Bridges/DITracy/ContainerPanel.php +++ b/src/Bridges/DITracy/ContainerPanel.php @@ -21,14 +21,11 @@ class ContainerPanel implements Tracy\IBarPanel { use Nette\SmartObject; - /** @var float|null */ - public static $compilationTime; + public static ?float $compilationTime = null; - /** @var Nette\DI\Container */ - private $container; + private Nette\DI\Container $container; - /** @var float|null */ - private $elapsedTime; + private ?float $elapsedTime; public function __construct(Container $container) diff --git a/src/DI/Autowiring.php b/src/DI/Autowiring.php index eef93109e..549a9bcdc 100644 --- a/src/DI/Autowiring.php +++ b/src/DI/Autowiring.php @@ -19,17 +19,16 @@ class Autowiring { use Nette\SmartObject; - /** @var ContainerBuilder */ - private $builder; + private ContainerBuilder $builder; /** @var array[] type => services, used by getByType() */ - private $highPriority = []; + private array $highPriority = []; /** @var array[] type => services, used by findByType() */ - private $lowPriority = []; + private array $lowPriority = []; /** @var string[] of classes excluded from autowiring */ - private $excludedClasses = []; + private array $excludedClasses = []; public function __construct(ContainerBuilder $builder) diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index eb8d7c1b2..563c04596 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -26,25 +26,20 @@ class Compiler DI = 'di'; /** @var CompilerExtension[] */ - private $extensions = []; + private array $extensions = []; - /** @var ContainerBuilder */ - private $builder; + private ContainerBuilder $builder; - /** @var array */ - private $config = []; + private array $config = []; /** @var array [section => array[]] */ - private $configs = []; + private array $configs = []; - /** @var string */ - private $sources = ''; + private string $sources = ''; - /** @var DependencyChecker */ - private $dependencies; + private DependencyChecker $dependencies; - /** @var string */ - private $className = 'Container'; + private string $className = 'Container'; public function __construct(ContainerBuilder $builder = null) diff --git a/src/DI/CompilerExtension.php b/src/DI/CompilerExtension.php index 29eef3688..1133e3958 100644 --- a/src/DI/CompilerExtension.php +++ b/src/DI/CompilerExtension.php @@ -19,17 +19,13 @@ abstract class CompilerExtension { use Nette\SmartObject; - /** @var Compiler */ - protected $compiler; + protected Compiler $compiler; - /** @var string */ - protected $name; + protected string $name; - /** @var array|object */ - protected $config = []; + protected array|object $config = []; - /** @var Nette\PhpGenerator\Closure */ - protected $initialization; + protected Nette\PhpGenerator\Closure $initialization; /** @return static */ diff --git a/src/DI/Config/DefinitionSchema.php b/src/DI/Config/DefinitionSchema.php index 1f910d7ab..d887283ae 100644 --- a/src/DI/Config/DefinitionSchema.php +++ b/src/DI/Config/DefinitionSchema.php @@ -24,8 +24,7 @@ class DefinitionSchema implements Schema { use Nette\SmartObject; - /** @var Nette\DI\ContainerBuilder */ - private $builder; + private Nette\DI\ContainerBuilder $builder; public function __construct(Nette\DI\ContainerBuilder $builder) diff --git a/src/DI/Config/Loader.php b/src/DI/Config/Loader.php index 6070d8971..0adc63528 100644 --- a/src/DI/Config/Loader.php +++ b/src/DI/Config/Loader.php @@ -22,16 +22,16 @@ class Loader private const INCLUDES_KEY = 'includes'; - private $adapters = [ + private array $adapters = [ 'php' => Adapters\PhpAdapter::class, 'neon' => Adapters\NeonAdapter::class, ]; - private $dependencies = []; + private array $dependencies = []; - private $loadedFiles = []; + private array $loadedFiles = []; - private $parameters = []; + private array $parameters = []; /** diff --git a/src/DI/Container.php b/src/DI/Container.php index e8ffe6c5c..4ab2ce34c 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -35,13 +35,12 @@ class Container protected $wiring = []; /** @var object[] service name => instance */ - private $instances = []; + private array $instances = []; /** @var array circular reference detector */ - private $creating; + private array $creating; - /** @var array */ - private $methods; + private array $methods; public function __construct(array $params = []) diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 6fd51b5ea..071032e8c 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -24,26 +24,21 @@ class ContainerBuilder THIS_SERVICE = 'self', THIS_CONTAINER = 'container'; - /** @var array */ - public $parameters = []; + public array $parameters = []; /** @var Definition[] */ - private $definitions = []; + private array $definitions = []; - /** @var array of alias => service */ - private $aliases = []; + /** alias => service */ + private array $aliases = []; - /** @var Autowiring */ - private $autowiring; + private Autowiring $autowiring; - /** @var bool */ - private $needsResolve = true; + private bool $needsResolve = true; - /** @var bool */ - private $resolving = false; + private bool $resolving = false; - /** @var array */ - private $dependencies = []; + private array $dependencies = []; public function __construct() diff --git a/src/DI/ContainerLoader.php b/src/DI/ContainerLoader.php index 255f04362..18a7079d6 100644 --- a/src/DI/ContainerLoader.php +++ b/src/DI/ContainerLoader.php @@ -19,11 +19,9 @@ class ContainerLoader { use Nette\SmartObject; - /** @var bool */ - private $autoRebuild = false; + private bool $autoRebuild = false; - /** @var string */ - private $tempDirectory; + private string $tempDirectory; public function __construct(string $tempDirectory, bool $autoRebuild = false) diff --git a/src/DI/Definitions/AccessorDefinition.php b/src/DI/Definitions/AccessorDefinition.php index cc0dd7541..896ae300c 100644 --- a/src/DI/Definitions/AccessorDefinition.php +++ b/src/DI/Definitions/AccessorDefinition.php @@ -21,8 +21,7 @@ final class AccessorDefinition extends Definition { private const METHOD_GET = 'get'; - /** @var Reference|null */ - private $reference; + private ?Reference $reference = null; /** @return static */ diff --git a/src/DI/Definitions/Definition.php b/src/DI/Definitions/Definition.php index 887a75c15..12db2ab20 100644 --- a/src/DI/Definitions/Definition.php +++ b/src/DI/Definitions/Definition.php @@ -19,19 +19,17 @@ abstract class Definition { use Nette\SmartObject; - /** @var string|null */ - private $name; + private ?string $name = null; - /** @var string|null class or interface name */ - private $type; + /** class or interface name */ + private ?string $type = null; - /** @var array */ - private $tags = []; + private array $tags = []; /** @var bool|string[] */ - private $autowired = true; + private bool|array $autowired = true; - /** @var callable|null */ + /** @var ?callable */ private $notifier; diff --git a/src/DI/Definitions/FactoryDefinition.php b/src/DI/Definitions/FactoryDefinition.php index 4aa2ee76c..e54780289 100644 --- a/src/DI/Definitions/FactoryDefinition.php +++ b/src/DI/Definitions/FactoryDefinition.php @@ -21,11 +21,9 @@ final class FactoryDefinition extends Definition { private const METHOD_CREATE = 'create'; - /** @var array */ - public $parameters = []; + public array $parameters = []; - /** @var Definition */ - private $resultDefinition; + private Definition $resultDefinition; public function __construct() diff --git a/src/DI/Definitions/LocatorDefinition.php b/src/DI/Definitions/LocatorDefinition.php index bab7ca7b9..cc09c955e 100644 --- a/src/DI/Definitions/LocatorDefinition.php +++ b/src/DI/Definitions/LocatorDefinition.php @@ -19,10 +19,9 @@ final class LocatorDefinition extends Definition { /** @var Reference[] */ - private $references = []; + private array $references = []; - /** @var string|null */ - private $tagged; + private ?string $tagged = null; /** @return static */ diff --git a/src/DI/Definitions/Reference.php b/src/DI/Definitions/Reference.php index 030748d39..9d225d598 100644 --- a/src/DI/Definitions/Reference.php +++ b/src/DI/Definitions/Reference.php @@ -21,8 +21,7 @@ final class Reference public const SELF = 'self'; - /** @var string */ - private $value; + private string $value; public static function fromType(string $value): self diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index d124cadc4..0ee0437fa 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -23,11 +23,10 @@ */ final class ServiceDefinition extends Definition { - /** @var Statement */ - private $factory; + private Statement $factory; /** @var Statement[] */ - private $setup = []; + private array $setup = []; public function __construct() diff --git a/src/DI/Definitions/Statement.php b/src/DI/Definitions/Statement.php index cb69303b1..b3adef2c9 100644 --- a/src/DI/Definitions/Statement.php +++ b/src/DI/Definitions/Statement.php @@ -22,11 +22,9 @@ final class Statement implements Nette\Schema\DynamicParameter { use Nette\SmartObject; - /** @var array */ - public $arguments; + public array $arguments; - /** @var string|array|Definition|Reference|null */ - private $entity; + private string|array|Definition|Reference|null $entity; /** diff --git a/src/DI/DependencyChecker.php b/src/DI/DependencyChecker.php index b62db69e7..2ed482718 100644 --- a/src/DI/DependencyChecker.php +++ b/src/DI/DependencyChecker.php @@ -24,8 +24,8 @@ class DependencyChecker public const VERSION = 1; - /** @var array of ReflectionClass|\ReflectionFunctionAbstract|string */ - private $dependencies = []; + /** @var array */ + private array $dependencies = []; /** diff --git a/src/DI/Extensions/DIExtension.php b/src/DI/Extensions/DIExtension.php index 1b94dedd9..263432c9d 100644 --- a/src/DI/Extensions/DIExtension.php +++ b/src/DI/Extensions/DIExtension.php @@ -17,17 +17,13 @@ */ final class DIExtension extends Nette\DI\CompilerExtension { - /** @var array */ - public $exportedTags = []; + public array $exportedTags = []; - /** @var array */ - public $exportedTypes = []; + public array $exportedTypes = []; - /** @var bool */ - private $debugMode; + private bool $debugMode; - /** @var float */ - private $time; + private float $time; public function __construct(bool $debugMode = false) @@ -36,21 +32,17 @@ public function __construct(bool $debugMode = false) $this->time = microtime(true); $this->config = new class { - /** @var ?bool */ - public $debugger; + public ?bool $debugger = null; /** @var string[] */ - public $excluded = []; + public array $excluded = []; - /** @var ?string */ - public $parentClass; + public ?string $parentClass = null; - /** @var object */ - public $export; + public object $export; }; $this->config->export = new class { - /** @var bool */ - public $parameters = true; + public bool $parameters = true; /** @var string[]|bool|null */ public $tags = true; diff --git a/src/DI/Extensions/ParametersExtension.php b/src/DI/Extensions/ParametersExtension.php index 7ecd5789c..3fef91245 100644 --- a/src/DI/Extensions/ParametersExtension.php +++ b/src/DI/Extensions/ParametersExtension.php @@ -19,13 +19,12 @@ final class ParametersExtension extends Nette\DI\CompilerExtension { /** @var string[] */ - public $dynamicParams = []; + public array $dynamicParams = []; /** @var string[][] */ public $dynamicValidators = []; - /** @var array */ - private $compilerConfig; + private array $compilerConfig; public function __construct(array &$compilerConfig) diff --git a/src/DI/Extensions/SearchExtension.php b/src/DI/Extensions/SearchExtension.php index 0b375e17d..728873323 100644 --- a/src/DI/Extensions/SearchExtension.php +++ b/src/DI/Extensions/SearchExtension.php @@ -20,11 +20,9 @@ */ final class SearchExtension extends Nette\DI\CompilerExtension { - /** @var array */ - private $classes = []; + private array $classes = []; - /** @var string */ - private $tempDir; + private string $tempDir; public function __construct(string $tempDir) diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index faaae0b08..dd5a1cb3d 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -23,11 +23,9 @@ class PhpGenerator { use Nette\SmartObject; - /** @var ContainerBuilder */ - private $builder; + private ContainerBuilder $builder; - /** @var string */ - private $className; + private ?string $className = null; public function __construct(ContainerBuilder $builder) diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 085a79b17..40075ff55 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -28,20 +28,16 @@ class Resolver { use Nette\SmartObject; - /** @var ContainerBuilder */ - private $builder; + private ContainerBuilder $builder; - /** @var Definition|null */ - private $currentService; + private ?Definition $currentService = null; - /** @var string|null */ - private $currentServiceType; + private ?string $currentServiceType = null; - /** @var bool */ - private $currentServiceAllowed = false; + private bool $currentServiceAllowed = false; - /** @var \SplObjectStorage circular reference detector */ - private $recursive; + /** circular reference detector */ + private \SplObjectStorage $recursive; public function __construct(ContainerBuilder $builder) diff --git a/tests/DI/Compiler.arguments.phpt b/tests/DI/Compiler.arguments.phpt index 176710d16..a760f7334 100644 --- a/tests/DI/Compiler.arguments.phpt +++ b/tests/DI/Compiler.arguments.phpt @@ -19,7 +19,7 @@ class Lorem public $args; - public $var = 123; + public int $var = 123; public function __construct() diff --git a/tests/DI/CompilerExtension.validateConfig.phpt b/tests/DI/CompilerExtension.validateConfig.phpt index d0bab69e5..44ac6f97a 100644 --- a/tests/DI/CompilerExtension.validateConfig.phpt +++ b/tests/DI/CompilerExtension.validateConfig.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php'; class MyExtension extends Nette\DI\CompilerExtension { - protected $name = 'my'; + protected string $name = 'my'; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9a7a63f32..531d91951 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -45,7 +45,7 @@ function test(string $title, Closure $function): void class Notes { - public static $notes = []; + public static array $notes = []; public static function add($message): void From a832782497d9351e9a13b0708ac6fcabf5394981 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 19:52:57 +0100 Subject: [PATCH 11/14] added PHP 8 typehints --- src/DI/Autowiring.php | 1 - src/DI/Compiler.php | 27 +++++++-------------- src/DI/CompilerExtension.php | 15 +++--------- src/DI/Config/Loader.php | 7 ++---- src/DI/Container.php | 29 +++++++---------------- src/DI/ContainerBuilder.php | 9 ++----- src/DI/ContainerLoader.php | 8 ++----- src/DI/Definitions/AccessorDefinition.php | 9 ++----- src/DI/Definitions/Definition.php | 29 +++++++---------------- src/DI/Definitions/FactoryDefinition.php | 9 +++---- src/DI/Definitions/ImportedDefinition.php | 3 +-- src/DI/Definitions/LocatorDefinition.php | 9 +++---- src/DI/Definitions/Reference.php | 2 +- src/DI/Definitions/ServiceDefinition.php | 27 ++++++--------------- src/DI/Definitions/Statement.php | 8 ++----- src/DI/DependencyChecker.php | 3 +-- src/DI/Extensions/InjectExtension.php | 19 ++++++--------- src/DI/Helpers.php | 18 ++++---------- src/DI/Resolver.php | 11 ++++----- src/DI/exceptions.php | 2 +- tests/DI/Container.errors.phpt | 4 ---- tests/bootstrap.php | 2 +- 22 files changed, 71 insertions(+), 180 deletions(-) diff --git a/src/DI/Autowiring.php b/src/DI/Autowiring.php index 549a9bcdc..3bbcaa6ee 100644 --- a/src/DI/Autowiring.php +++ b/src/DI/Autowiring.php @@ -39,7 +39,6 @@ public function __construct(ContainerBuilder $builder) /** * Resolves service name by type. - * @param bool $throw exception if service not found? * @throws MissingServiceException when not found * @throws ServiceCreationException when multiple found */ diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index 563c04596..9b5cca3c3 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -53,9 +53,8 @@ public function __construct(ContainerBuilder $builder = null) /** * Add custom configurator extension. - * @return static */ - public function addExtension(?string $name, CompilerExtension $extension) + public function addExtension(?string $name, CompilerExtension $extension): static { if ($name === null) { $name = '_' . count($this->extensions); @@ -87,8 +86,7 @@ public function getContainerBuilder(): ContainerBuilder } - /** @return static */ - public function setClassName(string $className) + public function setClassName(string $className): static { $this->className = $className; return $this; @@ -97,9 +95,8 @@ public function setClassName(string $className) /** * Adds new configuration. - * @return static */ - public function addConfig(array $config) + public function addConfig(array $config): static { foreach ($config as $section => $data) { $this->configs[$section][] = $data; @@ -111,9 +108,8 @@ public function addConfig(array $config) /** * Adds new configuration from file. - * @return static */ - public function loadConfig(string $file, Config\Loader $loader = null) + public function loadConfig(string $file, Config\Loader $loader = null): static { $sources = $this->sources . "// source: $file\n"; $loader = $loader ?: new Config\Loader; @@ -138,9 +134,8 @@ public function getConfig(): array /** * Sets the names of dynamic parameters. - * @return static */ - public function setDynamicParameterNames(array $names) + public function setDynamicParameterNames(array $names): static { assert($this->extensions[self::PARAMETERS] instanceof Extensions\ParametersExtension); $this->extensions[self::PARAMETERS]->dynamicParams = $names; @@ -151,9 +146,8 @@ public function setDynamicParameterNames(array $names) /** * Adds dependencies to the list. * @param array $deps of ReflectionClass|\ReflectionFunctionAbstract|string - * @return static */ - public function addDependencies(array $deps) + public function addDependencies(array $deps): static { $this->dependencies->add(array_filter($deps)); return $this; @@ -169,8 +163,7 @@ public function exportDependencies(): array } - /** @return static */ - public function addExportedTag(string $tag) + public function addExportedTag(string $tag): static { if (isset($this->extensions[self::DI])) { assert($this->extensions[self::DI] instanceof Extensions\DIExtension); @@ -180,8 +173,7 @@ public function addExportedTag(string $tag) } - /** @return static */ - public function addExportedType(string $type) + public function addExportedType(string $type): static { if (isset($this->extensions[self::DI])) { assert($this->extensions[self::DI] instanceof Extensions\DIExtension); @@ -261,9 +253,8 @@ private function processBeforeCompile(): void /** * Merges and validates configurations against scheme. - * @return array|object */ - private function processSchema(Schema\Schema $schema, array $configs, $name = null) + private function processSchema(Schema\Schema $schema, array $configs, $name = null): array|object { $processor = new Schema\Processor; $processor->onNewContext[] = function (Schema\Context $context) use ($name) { diff --git a/src/DI/CompilerExtension.php b/src/DI/CompilerExtension.php index 1133e3958..ef7c44980 100644 --- a/src/DI/CompilerExtension.php +++ b/src/DI/CompilerExtension.php @@ -28,8 +28,7 @@ abstract class CompilerExtension protected Nette\PhpGenerator\Closure $initialization; - /** @return static */ - public function setCompiler(Compiler $compiler, string $name) + public function setCompiler(Compiler $compiler, string $name): static { $this->initialization = new Nette\PhpGenerator\Closure; $this->compiler = $compiler; @@ -38,15 +37,8 @@ public function setCompiler(Compiler $compiler, string $name) } - /** - * @param array|object $config - * @return static - */ - public function setConfig($config) + public function setConfig(array|object $config): static { - if (!is_array($config) && !is_object($config)) { - throw new Nette\InvalidArgumentException; - } $this->config = $config; return $this; } @@ -54,9 +46,8 @@ public function setConfig($config) /** * Returns extension configuration. - * @return array|object */ - public function getConfig() + public function getConfig(): array|object { return $this->config; } diff --git a/src/DI/Config/Loader.php b/src/DI/Config/Loader.php index 0adc63528..066f3be60 100644 --- a/src/DI/Config/Loader.php +++ b/src/DI/Config/Loader.php @@ -104,10 +104,8 @@ public function expandIncludedFile(string $includedFile, string $mainFile): stri /** * Registers adapter for given file extension. - * @param string|Adapter $adapter - * @return static */ - public function addAdapter(string $extension, $adapter) + public function addAdapter(string $extension, string|Adapter $adapter): static { $this->adapters[strtolower($extension)] = $adapter; return $this; @@ -126,8 +124,7 @@ private function getAdapter(string $file): Adapter } - /** @return static */ - public function setParameters(array $params) + public function setParameters(array $params): static { $this->parameters = $params; return $this; diff --git a/src/DI/Container.php b/src/DI/Container.php index 4ab2ce34c..02f2392e6 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -62,16 +62,12 @@ public function getParameters(): array /** * Adds the service to the container. * @param object $service service or its factory - * @return static */ - public function addService(string $name, $service) + public function addService(string $name, object $service): static { $name = $this->aliases[$name] ?? $name; if (isset($this->instances[$name])) { throw new Nette\InvalidStateException("Service '$name' already exists."); - - } elseif (!is_object($service)) { - throw new Nette\InvalidArgumentException(sprintf("Service '%s' must be a object, %s given.", $name, gettype($service))); } $type = $service instanceof \Closure @@ -108,10 +104,9 @@ public function removeService(string $name): void /** * Gets the service object by name. - * @return object * @throws MissingServiceException */ - public function getService(string $name) + public function getService(string $name): object { if (!isset($this->instances[$name])) { if (isset($this->aliases[$name])) { @@ -125,10 +120,9 @@ public function getService(string $name) /** * Gets the service object by name. - * @return object * @throws MissingServiceException */ - public function getByName(string $name) + public function getByName(string $name): object { return $this->getService($name); } @@ -182,10 +176,9 @@ public function isCreated(string $name): bool /** * Creates new instance of the service. - * @return object * @throws MissingServiceException */ - public function createService(string $name, array $args = []) + public function createService(string $name, array $args = []): object { $name = $this->aliases[$name] ?? $name; $method = self::getMethodName($name); @@ -217,11 +210,9 @@ public function createService(string $name, array $args = []) /** * Resolves service by type. - * @param bool $throw exception if service doesn't exist? - * @return object|null service * @throws MissingServiceException */ - public function getByType(string $type, bool $throw = true) + public function getByType(string $type, bool $throw = true): ?object { $type = Helpers::normalizeClass($type); if (!empty($this->wiring[$type][0])) { @@ -287,10 +278,8 @@ public function findByTag(string $tag): array /** * Creates new instance using autowiring. - * @return object - * @throws Nette\InvalidArgumentException */ - public function createInstance(string $class, array $args = []) + public function createInstance(string $class, array $args = []): object { $rc = new \ReflectionClass($class); if (!$rc->isInstantiable()) { @@ -308,9 +297,8 @@ public function createInstance(string $class, array $args = []) /** * Calls all methods starting with with "inject" using autowiring. - * @param object $service */ - public function callInjects($service): void + public function callInjects(object $service): void { Extensions\InjectExtension::callInjects($this, $service); } @@ -318,9 +306,8 @@ public function callInjects($service): void /** * Calls method using autowiring. - * @return mixed */ - public function callMethod(callable $function, array $args = []) + public function callMethod(callable $function, array $args = []): mixed { return $function(...$this->autowireArguments(Nette\Utils\Callback::toReflection($function), $args)); } diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 071032e8c..f4f7a3abc 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -50,7 +50,6 @@ public function __construct() /** * Adds new service definition. - * @return Definitions\ServiceDefinition */ public function addDefinition(?string $name, Definition $definition = null): Definition { @@ -194,9 +193,8 @@ public function getAliases(): array /** * @param string[] $types - * @return static */ - public function addExcludedClasses(array $types) + public function addExcludedClasses(array $types): static { $this->needsResolve = true; $this->autowiring->addExcludedClasses($types); @@ -206,7 +204,6 @@ public function addExcludedClasses(array $types) /** * Resolves autowired service name by type. - * @param bool $throw exception if service doesn't exist? * @throws MissingServiceException */ public function getByType(string $type, bool $throw = false): ?string @@ -321,11 +318,9 @@ public function complete(): void /** * Adds item to the list of dependencies. - * @param \ReflectionClass|\ReflectionFunctionAbstract|string $dep - * @return static * @internal */ - public function addDependency($dep) + public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|string $dep): static { $this->dependencies[] = $dep; return $this; diff --git a/src/DI/ContainerLoader.php b/src/DI/ContainerLoader.php index 18a7079d6..8df7f0c0f 100644 --- a/src/DI/ContainerLoader.php +++ b/src/DI/ContainerLoader.php @@ -33,9 +33,8 @@ public function __construct(string $tempDirectory, bool $autoRebuild = false) /** * @param callable $generator function (Nette\DI\Compiler $compiler): string|null - * @param mixed $key */ - public function load(callable $generator, $key = null): string + public function load(callable $generator, mixed $key = null): string { $class = $this->getClassName($key); if (!class_exists($class, false)) { @@ -45,10 +44,7 @@ public function load(callable $generator, $key = null): string } - /** - * @param mixed $key - */ - public function getClassName($key): string + public function getClassName(mixed $key): string { return 'Container_' . substr(md5(serialize($key)), 0, 10); } diff --git a/src/DI/Definitions/AccessorDefinition.php b/src/DI/Definitions/AccessorDefinition.php index 896ae300c..49e6451cb 100644 --- a/src/DI/Definitions/AccessorDefinition.php +++ b/src/DI/Definitions/AccessorDefinition.php @@ -24,8 +24,7 @@ final class AccessorDefinition extends Definition private ?Reference $reference = null; - /** @return static */ - public function setImplement(string $type) + public function setImplement(string $type): static { if (!interface_exists($type)) { throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface '$type' not found."); @@ -53,11 +52,7 @@ public function getImplement(): ?string } - /** - * @param string|Reference $reference - * @return static - */ - public function setReference($reference) + public function setReference(string|Reference $reference): static { if ($reference instanceof Reference) { $this->reference = $reference; diff --git a/src/DI/Definitions/Definition.php b/src/DI/Definitions/Definition.php index 12db2ab20..c9a0ce119 100644 --- a/src/DI/Definitions/Definition.php +++ b/src/DI/Definitions/Definition.php @@ -34,10 +34,9 @@ abstract class Definition /** - * @return static * @internal This is managed by ContainerBuilder and should not be called by user */ - final public function setName(string $name) + final public function setName(string $name): static { if ($this->name) { throw new Nette\InvalidStateException('Name already has been set.'); @@ -53,8 +52,7 @@ final public function getName(): ?string } - /** @return static */ - protected function setType(?string $type) + protected function setType(?string $type): static { if ($this->autowired && $this->notifier && $this->type !== $type) { ($this->notifier)(); @@ -76,8 +74,7 @@ final public function getType(): ?string } - /** @return static */ - final public function setTags(array $tags) + final public function setTags(array $tags): static { $this->tags = $tags; return $this; @@ -90,29 +87,20 @@ final public function getTags(): array } - /** - * @param mixed $attr - * @return static - */ - final public function addTag(string $tag, $attr = true) + final public function addTag(string $tag, mixed $attr = true): static { $this->tags[$tag] = $attr; return $this; } - /** @return mixed */ - final public function getTag(string $tag) + final public function getTag(string $tag): mixed { return $this->tags[$tag] ?? null; } - /** - * @param bool|string|string[] $state - * @return static - */ - final public function setAutowired($state = true) + final public function setAutowired(bool|string|array $state = true): static { if ($this->notifier && $this->autowired !== $state) { ($this->notifier)(); @@ -125,14 +113,13 @@ final public function setAutowired($state = true) /** @return bool|string[] */ - final public function getAutowired() + final public function getAutowired(): bool|array { return $this->autowired; } - /** @return static */ - public function setExported(bool $state = true) + public function setExported(bool $state = true): static { return $this->addTag('nette.exported', $state); } diff --git a/src/DI/Definitions/FactoryDefinition.php b/src/DI/Definitions/FactoryDefinition.php index e54780289..313c1a537 100644 --- a/src/DI/Definitions/FactoryDefinition.php +++ b/src/DI/Definitions/FactoryDefinition.php @@ -32,8 +32,7 @@ public function __construct() } - /** @return static */ - public function setImplement(string $type) + public function setImplement(string $type): static { if (!interface_exists($type)) { throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface '$type' not found."); @@ -59,8 +58,7 @@ final public function getResultType(): ?string } - /** @return static */ - public function setResultDefinition(Definition $definition) + public function setResultDefinition(Definition $definition): static { $this->resultDefinition = $definition; return $this; @@ -74,8 +72,7 @@ public function getResultDefinition(): Definition } - /** @return static */ - public function setParameters(array $params) + public function setParameters(array $params): static { $this->parameters = $params; return $this; diff --git a/src/DI/Definitions/ImportedDefinition.php b/src/DI/Definitions/ImportedDefinition.php index d05948fcf..d02dc803a 100644 --- a/src/DI/Definitions/ImportedDefinition.php +++ b/src/DI/Definitions/ImportedDefinition.php @@ -18,8 +18,7 @@ */ final class ImportedDefinition extends Definition { - /** @return static */ - public function setType(?string $type) + public function setType(?string $type): static { return parent::setType($type); } diff --git a/src/DI/Definitions/LocatorDefinition.php b/src/DI/Definitions/LocatorDefinition.php index cc09c955e..6af63a43a 100644 --- a/src/DI/Definitions/LocatorDefinition.php +++ b/src/DI/Definitions/LocatorDefinition.php @@ -24,8 +24,7 @@ final class LocatorDefinition extends Definition private ?string $tagged = null; - /** @return static */ - public function setImplement(string $type) + public function setImplement(string $type): static { if (!interface_exists($type)) { throw new Nette\InvalidArgumentException(sprintf("Service '%s': Interface '%s' not found.", $this->getName(), $type)); @@ -58,8 +57,7 @@ public function getImplement(): ?string } - /** @return static */ - public function setReferences(array $references) + public function setReferences(array $references): static { $this->references = []; foreach ($references as $name => $ref) { @@ -78,8 +76,7 @@ public function getReferences(): array } - /** @return static */ - public function setTagged(?string $tagged) + public function setTagged(?string $tagged): static { $this->tagged = $tagged; return $this; diff --git a/src/DI/Definitions/Reference.php b/src/DI/Definitions/Reference.php index 9d225d598..d0b502ea1 100644 --- a/src/DI/Definitions/Reference.php +++ b/src/DI/Definitions/Reference.php @@ -24,7 +24,7 @@ final class Reference private string $value; - public static function fromType(string $value): self + public static function fromType(string $value): static { if (strpos($value, '\\') === false) { $value = '\\' . $value; diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index 0ee0437fa..baf5be304 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -35,18 +35,13 @@ public function __construct() } - /** @return static */ - public function setType(?string $type) + public function setType(?string $type): static { return parent::setType($type); } - /** - * @param string|array|Definition|Reference|Statement $factory - * @return static - */ - public function setFactory($factory, array $args = []) + public function setFactory(string|array|Definition|Reference|Statement $factory, array $args = []): static { $this->factory = $factory instanceof Statement ? $factory @@ -61,23 +56,20 @@ public function getFactory(): Statement } - /** @return string|array|Definition|Reference|null */ - public function getEntity() + public function getEntity(): string|array|Definition|Reference|null { return $this->factory->getEntity(); } - /** @return static */ - public function setArguments(array $args = []) + public function setArguments(array $args = []): static { $this->factory->arguments = $args; return $this; } - /** @return static */ - public function setArgument($key, $value) + public function setArgument($key, $value): static { $this->factory->arguments[$key] = $value; return $this; @@ -86,9 +78,8 @@ public function setArgument($key, $value) /** * @param Statement[] $setup - * @return static */ - public function setSetup(array $setup) + public function setSetup(array $setup): static { foreach ($setup as $v) { if (!$v instanceof Statement) { @@ -107,11 +98,7 @@ public function getSetup(): array } - /** - * @param string|array|Definition|Reference|Statement $entity - * @return static - */ - public function addSetup($entity, array $args = []) + public function addSetup(string|array|Definition|Reference|Statement $entity, array $args = []): static { $this->setup[] = $entity instanceof Statement ? $entity diff --git a/src/DI/Definitions/Statement.php b/src/DI/Definitions/Statement.php index b3adef2c9..e516b88da 100644 --- a/src/DI/Definitions/Statement.php +++ b/src/DI/Definitions/Statement.php @@ -27,10 +27,7 @@ final class Statement implements Nette\Schema\DynamicParameter private string|array|Definition|Reference|null $entity; - /** - * @param string|array|Definition|Reference|null $entity - */ - public function __construct($entity, array $arguments = []) + public function __construct(string|array|Definition|Reference|null $entity, array $arguments = []) { if ( $entity !== null @@ -62,8 +59,7 @@ public function __construct($entity, array $arguments = []) } - /** @return string|array|Definition|Reference|null */ - public function getEntity() + public function getEntity(): string|array|Definition|Reference|null { return $this->entity; } diff --git a/src/DI/DependencyChecker.php b/src/DI/DependencyChecker.php index 2ed482718..8bd1594eb 100644 --- a/src/DI/DependencyChecker.php +++ b/src/DI/DependencyChecker.php @@ -30,9 +30,8 @@ class DependencyChecker /** * Adds dependencies to the list. - * @return static */ - public function add(array $deps) + public function add(array $deps): static { $this->dependencies = array_merge($this->dependencies, $deps); return $this; diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index 098107f1f..2e69a0a36 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -129,14 +129,9 @@ public static function getInjectProperties(string $class): array /** * Calls all methods starting with with "inject" using autowiring. - * @param object $service */ - public static function callInjects(DI\Container $container, $service): void + public static function callInjects(DI\Container $container, object $service): void { - if (!is_object($service)) { - throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service))); - } - foreach (self::getInjectMethods(get_class($service)) as $method) { $container->callMethod([$service, $method]); } @@ -148,12 +143,12 @@ public static function callInjects(DI\Container $container, $service): void } - /** - * @param object|string $class - * @param DI\Container|DI\ContainerBuilder|null $container - */ - private static function checkType($class, string $name, ?string $type, $container): void - { + private static function checkType( + object|string $class, + string $name, + ?string $type, + DI\Container|DI\ContainerBuilder|null $container, + ): void { $propName = Reflection::toString(new \ReflectionProperty($class, $name)); if (!$type) { throw new Nette\InvalidStateException("Property $propName has no type hint."); diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index 690d1b1c7..dc8c12cf5 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -25,12 +25,9 @@ final class Helpers /** * Expands %placeholders%. - * @param mixed $var - * @param bool|array $recursive - * @return mixed * @throws Nette\InvalidArgumentException */ - public static function expand($var, array $params, $recursive = false) + public static function expand(mixed $var, array $params, bool|array $recursive = false): mixed { if (is_array($var)) { $res = []; @@ -102,10 +99,8 @@ public static function expand($var, array $params, $recursive = false) /** * Escapes '%' and '@' - * @param mixed $value - * @return mixed */ - public static function escape($value) + public static function escape(mixed $value): mixed { if (is_array($value)) { $res = []; @@ -146,10 +141,8 @@ public static function filterArguments(array $args): array /** * Replaces @extension with real extension name in service definition. - * @param mixed $config - * @return mixed */ - public static function prefixServiceName($config, string $namespace) + public static function prefixServiceName(mixed $config, string $namespace): mixed { if (is_string($config)) { if (strncmp($config, '@extension.', 10) === 0) { @@ -175,7 +168,6 @@ public static function prefixServiceName($config, string $namespace) /** * Returns an annotation value. - * @param \ReflectionFunctionAbstract|\ReflectionProperty|\ReflectionClass $ref */ public static function parseAnnotation(\Reflector $ref, string $name): ?string { @@ -219,11 +211,9 @@ public static function normalizeClass(string $type): string /** * Non data-loss type conversion. - * @param mixed $value - * @return mixed * @throws Nette\InvalidStateException */ - public static function convertType($value, string $type) + public static function convertType(mixed $value, string $type): mixed { if (is_scalar($value)) { $norm = ($value === false ? '0' : (string) $value); diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 40075ff55..4e60cc6bc 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -309,8 +309,8 @@ public function completeArguments(array $arguments): array } - /** @return string|array|Reference literal, Class, Reference, [Class, member], [, globalFunc], [Reference, member], [Statement, member] */ - private function normalizeEntity(Statement $statement) + /** Returns literal, Class, Reference, [Class, member], [, globalFunc], [Reference, member], [Statement, member] */ + private function normalizeEntity(Statement $statement): string|array|Reference { $entity = $statement->getEntity(); if (is_array($entity)) { @@ -394,10 +394,8 @@ public function getByType(string $type): Reference /** * Adds item to the list of dependencies. - * @param \ReflectionClass|\ReflectionFunctionAbstract|string $dep - * @return static */ - public function addDependency($dep) + public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|string $dep): static { $this->builder->addDependency($dep); return $this; @@ -529,9 +527,8 @@ public static function autowireArguments( * Resolves missing argument using autowiring. * @param (callable(string $type, bool $single): object|object[]|null) $getter * @throws ServiceCreationException - * @return mixed */ - private static function autowireArgument(\ReflectionParameter $parameter, callable $getter) + private static function autowireArgument(\ReflectionParameter $parameter, callable $getter): mixed { $types = array_diff(Reflection::getParameterTypes($parameter), ['null']); $type = count($types) === 1 ? reset($types) : null; diff --git a/src/DI/exceptions.php b/src/DI/exceptions.php index b441c5ed3..83fcb526d 100644 --- a/src/DI/exceptions.php +++ b/src/DI/exceptions.php @@ -25,7 +25,7 @@ class MissingServiceException extends Nette\InvalidStateException */ class ServiceCreationException extends Nette\InvalidStateException { - public function setMessage(string $message): self + public function setMessage(string $message): static { $this->message = $message; return $this; diff --git a/tests/DI/Container.errors.phpt b/tests/DI/Container.errors.phpt index 58e5d9ee1..244c46786 100644 --- a/tests/DI/Container.errors.phpt +++ b/tests/DI/Container.errors.phpt @@ -20,10 +20,6 @@ Assert::exception(function () use ($container, $service) { $container->addService('', $service); }, Nette\InvalidArgumentException::class, 'Service name must be a non-empty string.'); -Assert::exception(function () use ($container) { - $container->addService('one', null); -}, Nette\InvalidArgumentException::class, "Service 'one' must be a object, NULL given."); - Assert::exception(function () use ($container) { $container->getService('one'); }, Nette\DI\MissingServiceException::class, "Service 'one' not found."); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 531d91951..ae30cb39f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -63,7 +63,7 @@ public static function fetch(): array } -function createContainer($source, $config = null, $params = []): ?Nette\DI\Container +function createContainer($source, $config = null, array $params = []): ?Nette\DI\Container { $class = 'Container' . md5((string) lcg_value()); if ($source instanceof Nette\DI\ContainerBuilder) { From 43fdfa8b5c96f24e1e1ed62f4baadafe12a9599c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 11 Mar 2021 21:52:44 +0100 Subject: [PATCH 12/14] removed community health files --- .github/ISSUE_TEMPLATE/Bug_report.md | 19 ------------- .github/ISSUE_TEMPLATE/Feature_request.md | 9 ------ .github/ISSUE_TEMPLATE/Support_question.md | 12 -------- .github/ISSUE_TEMPLATE/Support_us.md | 21 -------------- .github/funding.yml | 2 -- .github/pull_request_template.md | 11 -------- contributing.md | 33 ---------------------- 7 files changed, 107 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/Bug_report.md delete mode 100644 .github/ISSUE_TEMPLATE/Feature_request.md delete mode 100644 .github/ISSUE_TEMPLATE/Support_question.md delete mode 100644 .github/ISSUE_TEMPLATE/Support_us.md delete mode 100644 .github/funding.yml delete mode 100644 .github/pull_request_template.md delete mode 100644 contributing.md diff --git a/.github/ISSUE_TEMPLATE/Bug_report.md b/.github/ISSUE_TEMPLATE/Bug_report.md deleted file mode 100644 index a4cd12634..000000000 --- a/.github/ISSUE_TEMPLATE/Bug_report.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -name: "🐛 Bug Report" -about: "If something isn't working as expected 🤔" - ---- - -Version: ?.?.? - -### Bug Description -... A clear and concise description of what the bug is. A good bug report shouldn't leave others needing to chase you up for more information. - -### Steps To Reproduce -... If possible a minimal demo of the problem ... - -### Expected Behavior -... A clear and concise description of what you expected to happen. - -### Possible Solution -... Only if you have suggestions on a fix for the bug diff --git a/.github/ISSUE_TEMPLATE/Feature_request.md b/.github/ISSUE_TEMPLATE/Feature_request.md deleted file mode 100644 index d2e219489..000000000 --- a/.github/ISSUE_TEMPLATE/Feature_request.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -name: "🚀 Feature Request" -about: "I have a suggestion (and may want to implement it) 🙂" - ---- - -- Is your feature request related to a problem? Please describe. -- Explain your intentions. -- It's up to you to make a strong case to convince the project's developers of the merits of this feature. diff --git a/.github/ISSUE_TEMPLATE/Support_question.md b/.github/ISSUE_TEMPLATE/Support_question.md deleted file mode 100644 index 75c48b6ed..000000000 --- a/.github/ISSUE_TEMPLATE/Support_question.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -name: "🤗 Support Question" -about: "If you have a question 💬, please check out our forum!" - ---- - ---------------^ Click "Preview" for a nicer view! -We primarily use GitHub as an issue tracker; for usage and support questions, please check out these resources below. Thanks! 😁. - -* Nette Forum: https://forum.nette.org -* Nette Gitter: https://gitter.im/nette/nette -* Slack (czech): https://pehapkari.slack.com/messages/C2R30BLKA diff --git a/.github/ISSUE_TEMPLATE/Support_us.md b/.github/ISSUE_TEMPLATE/Support_us.md deleted file mode 100644 index 92d8a4c3a..000000000 --- a/.github/ISSUE_TEMPLATE/Support_us.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -name: "❤️ Support us" -about: "If you would like to support our efforts in maintaining this project 🙌" - ---- - ---------------^ Click "Preview" for a nicer view! - -> https://nette.org/donate - -Help support Nette! - -We develop Nette Framework for more than 14 years. In order to make your life more comfortable. Nette cares about the safety of your sites. Nette saves you time. And gives job opportunities. - -Nette earns you money. And is absolutely free. - -To ensure future development and improving the documentation, we need your donation. - -Whether you are chief of IT company which benefits from Nette, or developer who goes for advice on our forum, if you like Nette, [please make a donation now](https://nette.org/donate). - -Thank you! diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 25adc9520..000000000 --- a/.github/funding.yml +++ /dev/null @@ -1,2 +0,0 @@ -github: dg -custom: "https://nette.org/donate" diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md deleted file mode 100644 index f8aa3f408..000000000 --- a/.github/pull_request_template.md +++ /dev/null @@ -1,11 +0,0 @@ -- bug fix / new feature? -- BC break? yes/no -- doc PR: nette/docs#??? - - diff --git a/contributing.md b/contributing.md deleted file mode 100644 index 184152c02..000000000 --- a/contributing.md +++ /dev/null @@ -1,33 +0,0 @@ -How to contribute & use the issue tracker -========================================= - -Nette welcomes your contributions. There are several ways to help out: - -* Create an issue on GitHub, if you have found a bug -* Write test cases for open bug issues -* Write fixes for open bug/feature issues, preferably with test cases included -* Contribute to the [documentation](https://nette.org/en/writing) - -Issues ------- - -Please **do not use the issue tracker to ask questions**. We will be happy to help you -on [Nette forum](https://forum.nette.org) or chat with us on [Gitter](https://gitter.im/nette/nette). - -A good bug report shouldn't leave others needing to chase you up for more -information. Please try to be as detailed as possible in your report. - -**Feature requests** are welcome. But take a moment to find out whether your idea -fits with the scope and aims of the project. It's up to *you* to make a strong -case to convince the project's developers of the merits of this feature. - -Contributing ------------- - -If you'd like to contribute, please take a moment to read [the contributing guide](https://nette.org/en/contributing). - -The best way to propose a feature is to discuss your ideas on [Nette forum](https://forum.nette.org) before implementing them. - -Please do not fix whitespace, format code, or make a purely cosmetic patch. - -Thanks! :heart: From 0d25785c7e24063d65177f8c7a7caa6365f2e02d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 3 Feb 2021 21:46:54 +0100 Subject: [PATCH 13/14] improved style of error messages WIP --- src/DI/Extensions/InjectExtension.php | 4 +- src/DI/Resolver.php | 18 ++-- tests/DI/Compiler.functions.phpt | 4 +- tests/DI/Compiler.generatedFactory.phpt | 9 +- tests/DI/Compiler.missingDefinition.phpt | 3 +- .../ContainerBuilder.autowiring.novalue.phpt | 6 +- tests/DI/ContainerBuilder.error.phpt | 6 +- tests/DI/ContainerBuilder.factory.error.phpt | 95 ++++++++++++++----- ...erBuilder.factory.resolveBuiltinTypes.phpt | 9 +- tests/DI/ContainerBuilder.recursive.phpt | 3 +- tests/DI/ContainerBuilder.selfdependency.phpt | 4 +- ...efinitions.AccessorDefinition.resolve.phpt | 6 +- ...Definitions.FactoryDefinition.resolve.phpt | 3 +- tests/DI/InjectExtension.errors.phpt | 6 +- .../DI/Resolver.autowireArguments.errors.phpt | 6 +- 15 files changed, 124 insertions(+), 58 deletions(-) diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index 2e69a0a36..b94af2984 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -153,9 +153,9 @@ private static function checkType( if (!$type) { throw new Nette\InvalidStateException("Property $propName has no type hint."); } elseif (!class_exists($type) && !interface_exists($type)) { - throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found. Check type and 'use' statements."); + throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found.\nCheck type and 'use' statements."); } elseif ($container && !$container->getByType($type, false)) { - throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found. Did you add it to configuration file?"); + throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found.\nDid you add it to configuration file?"); } } } diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 4e60cc6bc..f506d7832 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -269,8 +269,8 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo try { $arguments = $this->completeArguments($arguments); } catch (ServiceCreationException $e) { - if (!strpos($e->getMessage(), ' (used in')) { - $e->setMessage($e->getMessage() . " (used in {$this->entityToString($entity)})"); + if (!strpos($e->getMessage(), "\nUsed in")) { + $e->setMessage($e->getMessage() . "\nUsed in {$this->entityToString($entity)}."); } throw $e; } @@ -404,18 +404,18 @@ public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|strin private function completeException(\Exception $e, Definition $def): ServiceCreationException { - if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "Service '")) { + if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "(Service '")) { return $e; } $name = $def->getName(); $type = $def->getType(); if ($name && !ctype_digit($name)) { - $message = "Service '$name'" . ($type ? " (type of $type)" : '') . ': '; + $message = "(Service '$name'" . ($type ? " of type $type" : '') . ")\n"; } elseif ($type) { - $message = "Service of type $type: "; + $message = "(Service of type $type)\n"; } elseif ($def instanceof Definitions\ServiceDefinition && $def->getEntity()) { - $message = 'Service (' . $this->entityToString($def->getEntity()) . '): '; + $message = '(Service ' . $this->entityToString($def->getEntity()) . ")\n"; } else { $message = ''; } @@ -541,14 +541,14 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab } catch (MissingServiceException $e) { $res = null; } catch (ServiceCreationException $e) { - throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e); + throw new ServiceCreationException($e->getMessage() . "\nNeeded by $desc.", 0, $e); } if ($res !== null || $parameter->allowsNull()) { return $res; } elseif (class_exists($type) || interface_exists($type)) { - throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?"); + throw new ServiceCreationException("Service of type $type needed by $desc not found.\nDid you add it to configuration file?"); } else { - throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements."); + throw new ServiceCreationException("Class $type needed by $desc not found.\nCheck type hint and 'use' statements."); } } elseif ( diff --git a/tests/DI/Compiler.functions.phpt b/tests/DI/Compiler.functions.phpt index bf027462e..15881449b 100644 --- a/tests/DI/Compiler.functions.phpt +++ b/tests/DI/Compiler.functions.phpt @@ -85,4 +85,6 @@ Assert::exception(function () { services: - Service(bool(123, 10)) '); -}, Nette\InvalidStateException::class, 'Service of type Service: Function bool() expects at most 1 parameter, 2 given. (used in __construct())'); +}, Nette\InvalidStateException::class, '(Service of type Service) +Function bool() expects at most 1 parameter, 2 given. +Used in __construct().'); diff --git a/tests/DI/Compiler.generatedFactory.phpt b/tests/DI/Compiler.generatedFactory.phpt index 3a63e05d7..08cb09bf6 100644 --- a/tests/DI/Compiler.generatedFactory.phpt +++ b/tests/DI/Compiler.generatedFactory.phpt @@ -294,7 +294,8 @@ Assert::exception(function () { ->getResultDefinition() ->setFactory('Bad1'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type hint for \$bar in create() doesn't match type hint in Bad1 constructor."); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad2) +Type hint for \$bar in create() doesn't match type hint in Bad1 constructor."); @@ -317,7 +318,8 @@ Assert::exception(function () { ->getResultDefinition() ->setFactory('Bad3'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Unused parameter \$baz when implementing method create(), did you mean \$bar?"); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad4) +Unused parameter \$baz when implementing method create(), did you mean \$bar?"); @@ -340,7 +342,8 @@ Assert::exception(function () { ->getResultDefinition() ->setFactory('Bad5'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Unused parameter \$baz when implementing method create()."); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad6) +Unused parameter \$baz when implementing method create()."); diff --git a/tests/DI/Compiler.missingDefinition.phpt b/tests/DI/Compiler.missingDefinition.phpt index d98187b47..b03888158 100644 --- a/tests/DI/Compiler.missingDefinition.phpt +++ b/tests/DI/Compiler.missingDefinition.phpt @@ -22,4 +22,5 @@ Assert::throws(function () { services: foo: '); -}, Nette\InvalidStateException::class, "Service 'foo': Factory and type are missing in definition of service."); +}, Nette\InvalidStateException::class, "(Service 'foo') +Factory and type are missing in definition of service."); diff --git a/tests/DI/ContainerBuilder.autowiring.novalue.phpt b/tests/DI/ContainerBuilder.autowiring.novalue.phpt index 43bb07788..22da601ed 100644 --- a/tests/DI/ContainerBuilder.autowiring.novalue.phpt +++ b/tests/DI/ContainerBuilder.autowiring.novalue.phpt @@ -24,7 +24,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('foo')->setType('Foo'); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Foo): Parameter \$x in __construct() has no class type hint and no default value, so its value must be specified."); +}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Foo) +Parameter \$x in __construct() has no class type hint or default value, so its value must be specified."); class Bar @@ -38,7 +39,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('foo')->setType('Bar'); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Bar): Parameter \$x in __construct() has no class type hint and no default value, so its value must be specified."); +}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Bar) +Parameter \$x in __construct() has no class type hint or default value, so its value must be specified."); class Bar2 diff --git a/tests/DI/ContainerBuilder.error.phpt b/tests/DI/ContainerBuilder.error.phpt index b78631f2b..533e55fdd 100644 --- a/tests/DI/ContainerBuilder.error.phpt +++ b/tests/DI/ContainerBuilder.error.phpt @@ -20,7 +20,8 @@ $builder->addDefinition('one') Assert::exception(function () use ($builder) { $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of stdClass): Expected function, method or property name, '1234' given."); +}, Nette\InvalidStateException::class, "(Service 'one' of type stdClass) +Expected function, method or property name, '1234' given."); @@ -43,4 +44,5 @@ $builder->addDefinition('one') Assert::exception(function () use ($builder) { $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of stdClass): Missing argument for \$prop[]."); +}, Nette\InvalidStateException::class, "(Service 'one' of type stdClass) +Missing argument for \$prop[]."); diff --git a/tests/DI/ContainerBuilder.factory.error.phpt b/tests/DI/ContainerBuilder.factory.error.phpt index cd42e7a5b..adb2ebc56 100644 --- a/tests/DI/ContainerBuilder.factory.error.phpt +++ b/tests/DI/ContainerBuilder.factory.error.phpt @@ -25,7 +25,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition(null)->setFactory('Unknown'); $builder->complete(); -}, Nette\DI\ServiceCreationException::class, 'Service (Unknown::__construct()): Class Unknown not found.'); +}, Nette\DI\ServiceCreationException::class, '(Service Unknown::__construct()) +Class Unknown not found.'); Assert::exception(function () { @@ -33,7 +34,8 @@ Assert::exception(function () { $builder->addDefinition('one')->setFactory('@two'); $builder->addDefinition('two')->setFactory('Unknown'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'two': Class Unknown not found."); +}, Nette\InvalidStateException::class, "(Service 'two') +Class Unknown not found."); Assert::exception(function () { @@ -41,21 +43,24 @@ Assert::exception(function () { $builder->addDefinition('one')->setFactory(new Reference('two')); $builder->addDefinition('two')->setFactory('Unknown'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'two': Class Unknown not found."); +}, Nette\InvalidStateException::class, "(Service 'two') +Class Unknown not found."); Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('stdClass::foo'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one': Method stdClass::foo() is not callable."); +}, Nette\InvalidStateException::class, "(Service 'one') +Method stdClass::foo() is not callable."); Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Nette\DI\Container::foo'); // has __magic $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one': Method Nette\\DI\\Container::foo() is not callable."); +}, Nette\InvalidStateException::class, "(Service 'one') +Method Nette\\DI\\Container::foo() is not callable."); Assert::exception(function () { @@ -76,7 +81,8 @@ Assert::exception(function () { $builder->addFactoryDefinition('one') ->setImplement('Bad4'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Method create() has not return type hint or annotation @return."); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad4) +Method create() has not return type hint or annotation @return."); interface Bad5 @@ -103,7 +109,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Bad6::create'); $builder->complete(); -}, Nette\DI\ServiceCreationException::class, "Service 'one': Method Bad6::create() is not callable."); +}, Nette\DI\ServiceCreationException::class, "(Service 'one') +Method Bad6::create() is not callable."); class Bad7 @@ -117,7 +124,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Bad7::create'); $builder->complete(); -}, Nette\DI\ServiceCreationException::class, "Service 'one': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "(Service 'one') +Unknown service type, specify it or declare return type of factory."); class Bad8 @@ -131,7 +139,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setType('Bad8'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad8): Class Bad8 has private constructor."); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad8) +Class Bad8 has private constructor."); class Good @@ -146,14 +155,18 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Good', [new Statement('Unknown')]); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Unknown not found. (used in __construct())"); +}, Nette\InvalidStateException::class, "(Service 'one' of type Good) +Class Unknown not found. +Used in __construct()."); // fail in argument Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Good', [new Statement('Bad8')]); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Bad8 has private constructor. (used in __construct())"); +}, Nette\InvalidStateException::class, "(Service 'one' of type Good) +Class Bad8 has private constructor. +Used in __construct()."); abstract class Bad9 @@ -168,7 +181,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setType('Bad9'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one' (type of Bad9): Class Bad9 is abstract."); +}, Nette\InvalidStateException::class, "(Service 'one' of type Bad9) +Class Bad9 is abstract."); trait Bad10 @@ -183,7 +197,8 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Bad10::method'); $builder->complete(); -}, Nette\InvalidStateException::class, "Service 'one': Method Bad10::method() is not callable."); +}, Nette\InvalidStateException::class, "(Service 'one') +Method Bad10::method() is not callable."); class ConstructorParam @@ -208,7 +223,9 @@ services: b: stdClass bad: ConstructorParam '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (needed by \$x in __construct())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type ConstructorParam) +Multiple services of type stdClass found: a, b +Needed by \$x in __construct()."); // forced autowiring fail @@ -219,7 +236,9 @@ services: b: stdClass bad: ConstructorParam(@\stdClass) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (used in __construct())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type ConstructorParam) +Multiple services of type stdClass found: a, b +Used in __construct()."); // autowiring fail in chain @@ -230,7 +249,9 @@ services: b: stdClass bad: MethodParam()::foo() '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type MethodParam) +Multiple services of type stdClass found: a, b +Needed by \$x in foo()."); // forced autowiring fail in chain @@ -241,7 +262,9 @@ services: b: stdClass bad: MethodParam()::foo(@\stdClass) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (used in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type MethodParam) +Multiple services of type stdClass found: a, b +Used in foo()."); // autowiring fail in argument @@ -252,7 +275,10 @@ services: b: stdClass bad: Good(ConstructorParam()) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct()) (used in __construct())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Needed by \$x in ConstructorParam::__construct(). +Used in __construct()."); // forced autowiring fail in argument @@ -263,7 +289,9 @@ services: b: stdClass bad: Good(ConstructorParam(@\stdClass)) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in ConstructorParam::__construct()."); // autowiring fail in chain in argument @@ -274,7 +302,10 @@ services: b: stdClass bad: Good(MethodParam()::foo()) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo()) (used in __construct())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Needed by \$x in MethodParam::foo(). +Used in __construct()."); // forced autowiring fail in chain in argument @@ -285,7 +316,9 @@ services: b: stdClass bad: Good(MethodParam()::foo(@\stdClass)) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in foo()."); // forced autowiring fail in property passing @@ -299,7 +332,9 @@ services: setup: - $a = @\stdClass '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::\$a)"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in @bad::\$a."); // autowiring fail in rich property passing @@ -313,7 +348,9 @@ services: setup: - $a = MethodParam()::foo(@\stdClass) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in foo()."); // autowiring fail in method calling @@ -327,7 +364,9 @@ services: setup: - foo '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type MethodParam) +Multiple services of type stdClass found: a, b +Needed by \$x in foo()."); // forced autowiring fail in method calling @@ -341,7 +380,9 @@ services: setup: - bar(@\stdClass) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::bar())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in @bad::bar()."); // autowiring fail in rich method calling @@ -355,4 +396,6 @@ services: setup: - bar(MethodParam()::foo(@\stdClass)) '); -}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())"); +}, Nette\DI\ServiceCreationException::class, "(Service 'bad' of type Good) +Multiple services of type stdClass found: a, b +Used in foo()."); diff --git a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt index 4e15e3d9b..971452604 100644 --- a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt +++ b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt @@ -82,7 +82,8 @@ namespace $builder->addDefinition('a') ->setFactory('@factory::createArray'); $container = createContainer($builder); - }, Nette\DI\ServiceCreationException::class, "Service 'a': Class or interface 'array' not found. Is return type of A\\Factory::createArray() correct?"); + }, Nette\DI\ServiceCreationException::class, "(Service 'a') +Class or interface 'array' not found. Is return type of A\\Factory::createArray() correct?"); Assert::exception(function () { $builder = new DI\ContainerBuilder; @@ -91,7 +92,8 @@ namespace $builder->addDefinition('c') ->setFactory('@factory::createCallable'); $container = createContainer($builder); - }, Nette\DI\ServiceCreationException::class, "Service 'c': Class or interface 'callable' not found. Is return type of A\\Factory::createCallable() correct?"); + }, Nette\DI\ServiceCreationException::class, "(Service 'c') +Class or interface 'callable' not found. Is return type of A\\Factory::createCallable() correct?"); Assert::exception(function () { $builder = new DI\ContainerBuilder; @@ -100,7 +102,8 @@ namespace $builder->addDefinition('s') ->setFactory('@factory::createString'); $container = createContainer($builder); - }, Nette\DI\ServiceCreationException::class, "Service 's': Class or interface 'string' not found. Is return type of A\\Factory::createString() correct?"); + }, Nette\DI\ServiceCreationException::class, "(Service 's') +Class or interface 'string' not found. Is return type of A\\Factory::createString() correct?"); Assert::exception(function () { $builder = new DI\ContainerBuilder; diff --git a/tests/DI/ContainerBuilder.recursive.phpt b/tests/DI/ContainerBuilder.recursive.phpt index 50ca69c71..71d217844 100644 --- a/tests/DI/ContainerBuilder.recursive.phpt +++ b/tests/DI/ContainerBuilder.recursive.phpt @@ -29,4 +29,5 @@ $builder->addDefinition('two') Assert::exception(function () use ($builder) { $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'two': Circular reference detected for services: one, two."); +}, Nette\DI\ServiceCreationException::class, "(Service 'two') +Circular reference detected for services: one, two."); diff --git a/tests/DI/ContainerBuilder.selfdependency.phpt b/tests/DI/ContainerBuilder.selfdependency.phpt index d3bd587c5..027ce7064 100644 --- a/tests/DI/ContainerBuilder.selfdependency.phpt +++ b/tests/DI/ContainerBuilder.selfdependency.phpt @@ -27,4 +27,6 @@ $builder->addDefinition(null) Assert::exception(function () use ($builder) { createContainer($builder); -}, Nette\DI\ServiceCreationException::class, 'Service of type Foo: Service of type Foo needed by $foo in __construct() not found. Did you add it to configuration file?'); +}, Nette\DI\ServiceCreationException::class, '(Service of type Foo) +Service of type Foo needed by $foo in __construct() not found. +Did you add it to configuration file?'); diff --git a/tests/DI/Definitions.AccessorDefinition.resolve.phpt b/tests/DI/Definitions.AccessorDefinition.resolve.phpt index 2bb67e1d3..5c391946e 100644 --- a/tests/DI/Definitions.AccessorDefinition.resolve.phpt +++ b/tests/DI/Definitions.AccessorDefinition.resolve.phpt @@ -37,7 +37,8 @@ Assert::exception(function () { $resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder); $resolver->resolveDefinition($def); $resolver->completeDefinition($def); -}, Nette\DI\ServiceCreationException::class, 'Service of type Good1: Method get() has not return type hint or annotation @return.'); +}, Nette\DI\ServiceCreationException::class, '(Service of type Good1) +Method get() has not return type hint or annotation @return.'); Assert::noError(function () { @@ -66,4 +67,5 @@ Assert::exception(function () { $resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder); $resolver->resolveDefinition($def); $resolver->completeDefinition($def); -}, Nette\DI\ServiceCreationException::class, "Service of type Good2: Service of type 'stdClass' not found."); +}, Nette\DI\ServiceCreationException::class, "(Service of type Good2) +Service of type 'stdClass' not found."); diff --git a/tests/DI/Definitions.FactoryDefinition.resolve.phpt b/tests/DI/Definitions.FactoryDefinition.resolve.phpt index 5f06aefa9..91341aeb2 100644 --- a/tests/DI/Definitions.FactoryDefinition.resolve.phpt +++ b/tests/DI/Definitions.FactoryDefinition.resolve.phpt @@ -36,7 +36,8 @@ Assert::exception(function () { $def->setImplement('Good1'); $resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder); $resolver->resolveDefinition($def); -}, Nette\DI\ServiceCreationException::class, 'Service of type Good1: Method create() has not return type hint or annotation @return.'); +}, Nette\DI\ServiceCreationException::class, '(Service of type Good1) +Method create() has not return type hint or annotation @return.'); Assert::noError(function () { diff --git a/tests/DI/InjectExtension.errors.phpt b/tests/DI/InjectExtension.errors.phpt index 765e6094e..aa8c52b9e 100644 --- a/tests/DI/InjectExtension.errors.phpt +++ b/tests/DI/InjectExtension.errors.phpt @@ -46,7 +46,8 @@ services: factory: ServiceA inject: yes '); -}, InvalidStateException::class, 'Service of type DateTimeImmutable used in type hint at ServiceA::$a not found. Did you add it to configuration file?'); +}, InvalidStateException::class, 'Service of type DateTimeImmutable used in type hint at ServiceA::$a not found. +Did you add it to configuration file?'); Assert::exception(function () use ($compiler) { @@ -56,7 +57,8 @@ services: factory: ServiceB inject: yes '); -}, InvalidStateException::class, "Class or interface 'Unknown' used in type hint at ServiceB::\$a not found. Check type and 'use' statements."); +}, InvalidStateException::class, "Class or interface 'Unknown' used in type hint at ServiceB::\$a not found. +Check type and 'use' statements."); Assert::exception(function () use ($compiler) { diff --git a/tests/DI/Resolver.autowireArguments.errors.phpt b/tests/DI/Resolver.autowireArguments.errors.phpt index 7f5cdc078..06fa82f9d 100644 --- a/tests/DI/Resolver.autowireArguments.errors.phpt +++ b/tests/DI/Resolver.autowireArguments.errors.phpt @@ -15,12 +15,14 @@ require __DIR__ . '/../bootstrap.php'; Assert::exception(function () { Resolver::autowireArguments(new ReflectionFunction(function (stdClass $x) {}), [], function () {}); -}, Nette\DI\ServiceCreationException::class, 'Service of type stdClass needed by $x in {closure}() not found. Did you add it to configuration file?'); +}, Nette\DI\ServiceCreationException::class, 'Service of type stdClass needed by $x in {closure}() not found. +Did you add it to configuration file?'); Assert::exception(function () { Resolver::autowireArguments(new ReflectionFunction(function (Foo $x) {}), [], function () {}); -}, Nette\DI\ServiceCreationException::class, "Class Foo needed by \$x in {closure}() not found. Check type hint and 'use' statements."); +}, Nette\DI\ServiceCreationException::class, "Class Foo needed by \$x in {closure}() not found. +Check type hint and 'use' statements."); Assert::exception(function () { From 83bc422c7060e42d394147e2e872060d37e034ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Sat, 24 Apr 2021 21:18:56 +0200 Subject: [PATCH 14/14] Tracy panel: Better table responsivity (break service name). --- src/Bridges/DITracy/templates/ContainerPanel.panel.phtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bridges/DITracy/templates/ContainerPanel.panel.phtml b/src/Bridges/DITracy/templates/ContainerPanel.panel.phtml index 665919f87..bbacf20f8 100644 --- a/src/Bridges/DITracy/templates/ContainerPanel.panel.phtml +++ b/src/Bridges/DITracy/templates/ContainerPanel.panel.phtml @@ -39,7 +39,7 @@ use Tracy\Helpers; Name - Autowired + ATW Service Tags @@ -49,7 +49,7 @@ use Tracy\Helpers; - $name" : Helpers::escapeHtml($name) ?> + $name" : str_replace('.', '.', Helpers::escapeHtml($name)) ?>