Skip to content

Commit

Permalink
Add ShouldNotBeReadonly assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Geert Broekmans committed Feb 28, 2024
1 parent 68ed6b6 commit b71de31
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
dependencies:
- "highest"
steps:
Expand Down Expand Up @@ -60,6 +61,7 @@ jobs:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
dependencies:
- "highest"
steps:
Expand Down Expand Up @@ -95,6 +97,7 @@ jobs:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
dependencies:
- "highest"
steps:
Expand Down Expand Up @@ -132,6 +135,7 @@ jobs:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
dependencies:
- "highest"
steps:
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.10.15
* Add `shouldNotBeReadonly` assertion.

## 0.10.14
* Add `shouldInclude()` and `shouldNotInclude` assertions.
* Detect catch blocks in dependency assertions.
Expand Down
2 changes: 2 additions & 0 deletions docs/documentation/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ It asserts that the selected classes are **interfaces**.
## shouldBeReadonly()
It asserts that the selected classes are declared as **readonly**.

Also available: `shouldNotBeReadonly()`

## shouldHaveOnlyOnePublicMethod()
It asserts that the selected classes only have **one public method** (besides constructor).

Expand Down
6 changes: 6 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ services:
tags:
- phpstan.rules.rule

# ShouldNotBeReadonly rules
-
class: PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly\IsReadonlyRule
tags:
- phpstan.rules.rule

# ShouldBeAbstract rules
-
class: PHPat\Rule\Assertion\Declaration\ShouldBeAbstract\AbstractRule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly;

use PHPat\Rule\Extractor\Declaration\ReadonlyExtractor;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;

/**
* @implements Rule<InClassNode>
*/
final class IsReadonlyRule extends ShouldNotBeReadonly implements Rule
{
use ReadonlyExtractor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly;

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\Rules\RuleError;
use PHPStan\Type\FileTypeMapper;

abstract class ShouldNotBeReadonly extends DeclarationAssertion
{
use ValidationTrait;

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

/**
* @param array<string> $tips
* @return array<RuleError>
*/
protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array
{
return $this->applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params);
}

protected function getMessage(string $ruleName, string $subject, array $params = []): string
{
return $this->prepareMessage(
$ruleName,
sprintf('%s should not be readonly', $subject)
);
}
}
8 changes: 8 additions & 0 deletions src/Test/Builder/AssertionStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethod\ShouldHaveOnlyOnePublicMethod;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract\ShouldNotBeAbstract;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal\ShouldNotBeFinal;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly\ShouldNotBeReadonly;
use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CanOnlyDepend;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend;
Expand Down Expand Up @@ -52,6 +53,13 @@ public function shouldBeReadonly(): Rule
return new BuildStep($this->rule);
}

public function shouldNotBeReadonly(): Rule
{
$this->rule->assertion = ShouldNotBeReadonly::class;

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

public function shouldBeFinal(): Rule
{
$this->rule->assertion = ShouldBeFinal::class;
Expand Down

0 comments on commit b71de31

Please sign in to comment.