Skip to content
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
30 changes: 25 additions & 5 deletions src/DependencyInjection/ConditionalTagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace PHPStan\DependencyInjection;

use InvalidArgumentException;
use Nette;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Override;
use PHPStan\ShouldNotHappenException;
Expand All @@ -23,7 +25,11 @@ public function getConfigSchema(): Nette\Schema\Schema
$tags = array_values(ValidateServiceTagsExtension::INTERFACE_TAG_MAPPING);

return Expect::arrayOf(Expect::structure(
array_fill_keys($tags, Expect::anyOf(Expect::bool(), Expect::listOf(Expect::bool()))),
array_fill_keys($tags, Expect::anyOf(
Expect::bool(),
Expect::type(Statement::class),
Expect::listOf(Expect::anyOf(Expect::bool(), Expect::type(Statement::class))),
)),
)->min(1));
}

Expand All @@ -41,10 +47,11 @@ 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) {
$parameter = is_array($parameter)
? array_reduce($parameter, fn ($carry, $item) => $carry && $this->resolveValue($item), true)
: $this->resolveValue($parameter);

if ($parameter) {
$service->addTag($tag);
continue;
}
Expand All @@ -53,4 +60,17 @@ public function beforeCompile(): void
}
}

public function resolveValue(mixed $parameter): bool
{
if (!$parameter instanceof Statement) {
return (bool) $parameter;
}

if ($parameter->getEntity() === 'not') {
return ! (bool) $parameter->arguments[0];
}

throw new InvalidArgumentException('Unsupported Statement.');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public function testConditionalTags(): void
$enabledServices = array_map(static fn ($service) => get_class($service), $enabledServices);
$this->assertNotContains(TestedConditionalServiceDisabled::class, $enabledServices);
$this->assertContains(TestedConditionalServiceEnabled::class, $enabledServices);
$this->assertContains(TestedConditionalServiceNotDisabled::class, $enabledServices);
$this->assertNotContains(TestedConditionalServiceNotEnabled::class, $enabledServices);
$this->assertNotContains(TestedConditionalServiceDisabledDisabled::class, $enabledServices);
$this->assertNotContains(TestedConditionalServiceDisabledEnabled::class, $enabledServices);
$this->assertNotContains(TestedConditionalServiceEnabledDisabled::class, $enabledServices);
$this->assertContains(TestedConditionalServiceEnabledEnabled::class, $enabledServices);
$this->assertContains(TestedConditionalServiceEnabledNotDisabled::class, $enabledServices);
$this->assertNotContains(TestedConditionalServiceEnabledNotEnabled::class, $enabledServices);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node>
*/
class TestedConditionalServiceEnabledNotDisabled implements Rule
{

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
return [];
}

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

namespace PHPStan\DependencyInjection;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node>
*/
class TestedConditionalServiceEnabledNotEnabled implements Rule
{

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
return [];
}

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

namespace PHPStan\DependencyInjection;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node>
*/
class TestedConditionalServiceNotDisabled implements Rule
{

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
return [];
}

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

namespace PHPStan\DependencyInjection;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node>
*/
class TestedConditionalServiceNotEnabled implements Rule
{

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
return [];
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/DependencyInjection/conditionalTags.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ conditionalTags:
phpstan.rules.rule: %disabled%
PHPStan\DependencyInjection\TestedConditionalServiceEnabled:
phpstan.rules.rule: %enabled%
PHPStan\DependencyInjection\TestedConditionalServiceNotDisabled:
phpstan.rules.rule: not(%disabled%)
PHPStan\DependencyInjection\TestedConditionalServiceNotEnabled:
phpstan.rules.rule: not(%enabled%)
PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled:
phpstan.rules.rule: [%disabled%, %disabled%]
PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled:
Expand All @@ -19,11 +23,19 @@ conditionalTags:
phpstan.rules.rule: [%enabled%, %disabled%]
PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled:
phpstan.rules.rule: [%enabled%, %enabled%]
PHPStan\DependencyInjection\TestedConditionalServiceEnabledNotEnabled:
phpstan.rules.rule: [%enabled%, not(%enabled%)]
PHPStan\DependencyInjection\TestedConditionalServiceEnabledNotDisabled:
phpstan.rules.rule: [%enabled%, not(%disabled%)]

services:
- PHPStan\DependencyInjection\TestedConditionalServiceDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceNotDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceNotEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceDisabledEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledDisabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledNotEnabled
- PHPStan\DependencyInjection\TestedConditionalServiceEnabledNotDisabled
Loading