From c1dba876cbc7e55d30d38d3cf9ab90f196a38895 Mon Sep 17 00:00:00 2001 From: Martin Jonas Date: Mon, 5 Sep 2022 10:52:07 +0200 Subject: [PATCH] ConditionalTagsExtension: Multiple conditions support --- .../ConditionalTagsExtension.php | 7 +++- .../ConditionalTagsExtensionTest.php | 35 +++++++++++++++++++ .../TestedConditionalServiceDisabled.php | 8 +++++ ...stedConditionalServiceDisabledDisabled.php | 8 +++++ ...estedConditionalServiceDisabledEnabled.php | 8 +++++ .../TestedConditionalServiceEnabled.php | 8 +++++ ...estedConditionalServiceEnabledDisabled.php | 8 +++++ ...TestedConditionalServiceEnabledEnabled.php | 8 +++++ .../DependencyInjection/conditionalTags.neon | 29 +++++++++++++++ 9 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/DependencyInjection/ConditionalTagsExtensionTest.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabled.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabledDisabled.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabledEnabled.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceEnabled.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceEnabledDisabled.php create mode 100644 tests/PHPStan/DependencyInjection/TestedConditionalServiceEnabledEnabled.php create mode 100644 tests/PHPStan/DependencyInjection/conditionalTags.neon diff --git a/src/DependencyInjection/ConditionalTagsExtension.php b/src/DependencyInjection/ConditionalTagsExtension.php index ff811f9874..9d4137dcf3 100644 --- a/src/DependencyInjection/ConditionalTagsExtension.php +++ b/src/DependencyInjection/ConditionalTagsExtension.php @@ -12,7 +12,9 @@ use PHPStan\PhpDoc\TypeNodeResolverExtension; use PHPStan\Rules\RegistryFactory as RuleRegistryFactory; use PHPStan\ShouldNotHappenException; +use function array_reduce; use function count; +use function is_array; use function sprintf; class ConditionalTagsExtension extends CompilerExtension @@ -20,7 +22,7 @@ class ConditionalTagsExtension extends CompilerExtension public function getConfigSchema(): Nette\Schema\Schema { - $bool = Expect::bool(); + $bool = Expect::anyOf(Expect::bool(), Expect::listOf(Expect::bool())); return Expect::arrayOf(Expect::structure([ BrokerFactory::PROPERTIES_CLASS_REFLECTION_EXTENSION_TAG => $bool, BrokerFactory::METHODS_CLASS_REFLECTION_EXTENSION_TAG => $bool, @@ -51,6 +53,9 @@ public function beforeCompile(): void } foreach ($services as $service) { foreach ($tags as $tag => $parameter) { + if (is_array($parameter)) { + $parameter = array_reduce($parameter, static fn ($carry, $item) => $carry && (bool) $item, true); + } if ((bool) $parameter) { $service->addTag($tag); continue; diff --git a/tests/PHPStan/DependencyInjection/ConditionalTagsExtensionTest.php b/tests/PHPStan/DependencyInjection/ConditionalTagsExtensionTest.php new file mode 100644 index 0000000000..a244aa9441 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/ConditionalTagsExtensionTest.php @@ -0,0 +1,35 @@ +getServicesByTag(RuleRegistryFactory::RULE_TAG); + $enabledServices = array_map(static fn ($service) => get_class($service), $enabledServices); + $this->assertNotContains(TestedConditionalServiceDisabled::class, $enabledServices); + $this->assertContains(TestedConditionalServiceEnabled::class, $enabledServices); + $this->assertNotContains(TestedConditionalServiceDisabledDisabled::class, $enabledServices); + $this->assertNotContains(TestedConditionalServiceDisabledEnabled::class, $enabledServices); + $this->assertNotContains(TestedConditionalServiceEnabledDisabled::class, $enabledServices); + $this->assertContains(TestedConditionalServiceEnabledEnabled::class, $enabledServices); + } + + /** + * @return string[] + */ + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/conditionalTags.neon', + ]; + } + +} diff --git a/tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabled.php b/tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabled.php new file mode 100644 index 0000000000..0d0af6673c --- /dev/null +++ b/tests/PHPStan/DependencyInjection/TestedConditionalServiceDisabled.php @@ -0,0 +1,8 @@ +