Skip to content

ConditionalTagsExtension: Multiple conditions support #1697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/DependencyInjection/ConditionalTagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
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
{

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,
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/DependencyInjection/ConditionalTagsExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use PHPStan\Rules\RegistryFactory as RuleRegistryFactory;
use PHPStan\Testing\PHPStanTestCase;
use function array_map;
use function get_class;

class ConditionalTagsExtensionTest extends PHPStanTestCase
{

public function testConditionalTags(): void
{
$enabledServices = self::getContainer()->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',
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceDisabled
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceDisabledDisabled
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceDisabledEnabled
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceEnabled
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceEnabledDisabled
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

class TestedConditionalServiceEnabledEnabled
{

}
29 changes: 29 additions & 0 deletions tests/PHPStan/DependencyInjection/conditionalTags.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
parameters:
enabled: true
disabled: false

parametersSchema:
enabled: bool()
disabled: bool()

conditionalTags:
PHPStan\DependencyInjection\TestedConditionalServiceDisabled:
phpstan.rules.rule: %disabled%
PHPStan\DependencyInjection\TestedConditionalServiceEnabled:
phpstan.rules.rule: %enabled%
PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled:
phpstan.rules.rule: [%disabled%, %disabled%]
PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled:
phpstan.rules.rule: [%disabled%, %enabled%]
PHPStan\DependencyInjection\TestedConditionalServiceEnabledDisabled:
phpstan.rules.rule: [%enabled%, %disabled%]
PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled:
phpstan.rules.rule: [%enabled%, %enabled%]

services:
- PHPStan\DependencyInjection\TestedConditionalServiceDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled