Skip to content

Commit e93f75a

Browse files
MartinMystikJonasondrejmirtes
authored andcommitted
ConditionalTagsExtension: Multiple conditions support
1 parent d61b9f9 commit e93f75a

9 files changed

+118
-1
lines changed

src/DependencyInjection/ConditionalTagsExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
use PHPStan\PhpDoc\TypeNodeResolverExtension;
1313
use PHPStan\Rules\RegistryFactory as RuleRegistryFactory;
1414
use PHPStan\ShouldNotHappenException;
15+
use function array_reduce;
1516
use function count;
17+
use function is_array;
1618
use function sprintf;
1719

1820
class ConditionalTagsExtension extends CompilerExtension
1921
{
2022

2123
public function getConfigSchema(): Nette\Schema\Schema
2224
{
23-
$bool = Expect::bool();
25+
$bool = Expect::anyOf(Expect::bool(), Expect::listOf(Expect::bool()));
2426
return Expect::arrayOf(Expect::structure([
2527
BrokerFactory::PROPERTIES_CLASS_REFLECTION_EXTENSION_TAG => $bool,
2628
BrokerFactory::METHODS_CLASS_REFLECTION_EXTENSION_TAG => $bool,
@@ -51,6 +53,9 @@ public function beforeCompile(): void
5153
}
5254
foreach ($services as $service) {
5355
foreach ($tags as $tag => $parameter) {
56+
if (is_array($parameter)) {
57+
$parameter = array_reduce($parameter, static fn ($carry, $item) => $carry && (bool) $item, true);
58+
}
5459
if ((bool) $parameter) {
5560
$service->addTag($tag);
5661
continue;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use PHPStan\Rules\RegistryFactory as RuleRegistryFactory;
6+
use PHPStan\Testing\PHPStanTestCase;
7+
use function array_map;
8+
use function get_class;
9+
10+
class ConditionalTagsExtensionTest extends PHPStanTestCase
11+
{
12+
13+
public function testConditionalTags(): void
14+
{
15+
$enabledServices = self::getContainer()->getServicesByTag(RuleRegistryFactory::RULE_TAG);
16+
$enabledServices = array_map(static fn ($service) => get_class($service), $enabledServices);
17+
$this->assertNotContains(TestedConditionalServiceDisabled::class, $enabledServices);
18+
$this->assertContains(TestedConditionalServiceEnabled::class, $enabledServices);
19+
$this->assertNotContains(TestedConditionalServiceDisabledDisabled::class, $enabledServices);
20+
$this->assertNotContains(TestedConditionalServiceDisabledEnabled::class, $enabledServices);
21+
$this->assertNotContains(TestedConditionalServiceEnabledDisabled::class, $enabledServices);
22+
$this->assertContains(TestedConditionalServiceEnabledEnabled::class, $enabledServices);
23+
}
24+
25+
/**
26+
* @return string[]
27+
*/
28+
public static function getAdditionalConfigFiles(): array
29+
{
30+
return [
31+
__DIR__ . '/conditionalTags.neon',
32+
];
33+
}
34+
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceDisabled
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceDisabledDisabled
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceDisabledEnabled
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceEnabled
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceEnabledDisabled
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
class TestedConditionalServiceEnabledEnabled
6+
{
7+
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
parameters:
2+
enabled: true
3+
disabled: false
4+
5+
parametersSchema:
6+
enabled: bool()
7+
disabled: bool()
8+
9+
conditionalTags:
10+
PHPStan\DependencyInjection\TestedConditionalServiceDisabled:
11+
phpstan.rules.rule: %disabled%
12+
PHPStan\DependencyInjection\TestedConditionalServiceEnabled:
13+
phpstan.rules.rule: %enabled%
14+
PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled:
15+
phpstan.rules.rule: [%disabled%, %disabled%]
16+
PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled:
17+
phpstan.rules.rule: [%disabled%, %enabled%]
18+
PHPStan\DependencyInjection\TestedConditionalServiceEnabledDisabled:
19+
phpstan.rules.rule: [%enabled%, %disabled%]
20+
PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled:
21+
phpstan.rules.rule: [%enabled%, %enabled%]
22+
23+
services:
24+
- PHPStan\DependencyInjection\TestedConditionalServiceDisabled
25+
- PHPStan\DependencyInjection\TestedConditionalServiceEnabled
26+
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled
27+
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled
28+
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledDisabled
29+
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled

0 commit comments

Comments
 (0)