Skip to content
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

[DRAFT] Add shouldBeImmutable assertion #224

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 12 additions & 2 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ services:
show_rule_names: %phpat.show_rule_names%,
)

# # # # # DECLARATION RULES # # # # #
# # # # # DECLARATION RULES # # # # #

# ShouldBeImmutable rules
-
class: PHPat\Rule\Assertion\Declaration\ShouldBeImmutable\PropertyAssignationRule
tags:
- phpstan.rules.rule
-
class: PHPat\Rule\Assertion\Declaration\ShouldBeImmutable\MutablePropertyRule
tags:
- phpstan.rules.rule

# ShouldBeAbstract rules
-
Expand All @@ -42,7 +52,7 @@ services:
tags:
- phpstan.rules.rule

# # # # # RELATION RULES # # # # #
# # # # # RELATION RULES # # # # #

# ShouldImplement rules
-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldBeImmutable;

use PHPat\Rule\Extractor\Declaration\MutablePropertyExtractor;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Rules\Rule;

/**
* @implements Rule<ClassPropertyNode>
*/
class MutablePropertyRule extends ShouldBeImmutable implements Rule
{
use MutablePropertyExtractor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldBeImmutable;

use PHPat\Rule\Extractor\Declaration\PropertyAssignedOutOfConstructorExtractor;
use PhpParser\Node\Expr\Assign;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Assign>
*/
class PropertyAssignationRule extends ShouldBeImmutable implements Rule
{
use PropertyAssignedOutOfConstructorExtractor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldBeImmutable;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\DeclarationAssertion;
use PHPat\Rule\Assertion\Declaration\ValidationTrait;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\FileTypeMapper;

abstract class ShouldBeImmutable extends DeclarationAssertion
{
use ValidationTrait;

public function __construct(
StatementBuilderFactory $statementBuilderFactory,
Configuration $configuration,
ReflectionProvider $reflectionProvider,
FileTypeMapper $fileTypeMapper
) {
parent::__construct(
__CLASS__,
$statementBuilderFactory,
$configuration,
$reflectionProvider,
$fileTypeMapper,
);
}

protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array
{
return $this->applyShould($ruleName, $subject, $meetsDeclaration, $tips);
}

protected function getMessage(string $ruleName, string $subject): string
{
return $this->prepareMessage(
$ruleName,
sprintf('%s should be immutable', $subject)
);
}
}
34 changes: 34 additions & 0 deletions src/Rule/Extractor/Declaration/MutablePropertyExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PHPat\Rule\Extractor\Declaration;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;

trait MutablePropertyExtractor
{
public function getNodeType(): string
{
return ClassPropertyNode::class;
}

/**
* @param ClassPropertyNode $node
*/
protected function meetsDeclaration(Node $node, Scope $scope): bool
{
$class = $scope->getClassReflection();
if ($class && $class->isReadOnly()) {
return true;
}

if (!$node->isPublic() || $node->isReadonly()) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace PHPat\Rule\Extractor\Declaration;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\PropertyAssignNode;

trait PropertyAssignedOutOfConstructorExtractor
{
public function getNodeType(): string
{
return PropertyAssignNode::class;
}

/**
* @param PropertyAssignNode $node
*/
protected function meetsDeclaration(Node $node, Scope $scope): bool
{
$functionReflection = $scope->getFunction();
if ($functionReflection === null || $functionReflection->getName() === '__construct') {
return true;
}

return false;
}
}
8 changes: 8 additions & 0 deletions src/Test/Builder/AssertionStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPat\Rule\Assertion\Declaration\ShouldBeAbstract\ShouldBeAbstract;
use PHPat\Rule\Assertion\Declaration\ShouldBeFinal\ShouldBeFinal;
use PHPat\Rule\Assertion\Declaration\ShouldBeImmutable\ShouldBeImmutable;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract\ShouldNotBeAbstract;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal\ShouldNotBeFinal;
use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CanOnlyDepend;
Expand All @@ -18,6 +19,13 @@

class AssertionStep extends AbstractStep
{
public function shouldBeImmutable(): Rule
{
$this->rule->assertion = ShouldBeImmutable::class;

return new BuildStep($this->rule);
}

public function shouldBeAbstract(): Rule
{
$this->rule->assertion = ShouldBeAbstract::class;
Expand Down
7 changes: 7 additions & 0 deletions tests/architecture/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public function test_configuration_is_final(): Rule
->classes(Selector::classname(Configuration::class))
->shouldBeFinal();
}

public function test_configuration_is_immutable(): Rule
{
return PHPat::rule()
->classes(Selector::classname(Configuration::class))
->shouldBeImmutable();
}
}
Loading