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 cb3b200
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 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
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
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)
);
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/Simple/SimpleReadonlyClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\fixtures\Simple;

if (PHP_VERSION < 80200) {
class SimpleReadonlyClass {}
} else {
readonly class SimpleReadonlyClass {}
}
53 changes: 53 additions & 0 deletions tests/unit/rules/ShouldNotBeReadonly/ReadonlyClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldNotBeReadonly;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly\IsReadonlyRule;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly\ShouldNotBeReadonly;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\FixtureClass;
use Tests\PHPat\fixtures\Simple\SimpleReadonlyClass;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<IsReadonlyRule>
* @internal
* @coversNothing
*/
class ReadonlyClassTest extends RuleTestCase
{
public const RULE_NAME = 'test_FixtureClassShouldNotBeReadonly';

public function testRule(): void
{
if (PHP_VERSION_ID < 80200) {
self::markTestSkipped('PHP version does not support readonly classes');
}

$this->analyse(['tests/fixtures/Simple/SimpleReadonlyClass.php'], [
[sprintf('%s should not be readonly', SimpleReadonlyClass::class), 29],

Check failure on line 33 in tests/unit/rules/ShouldNotBeReadonly/ReadonlyClassTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, highest)

Class Tests\PHPat\fixtures\Simple\SimpleReadonlyClass not found.
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldNotBeReadonly::class,
[new Classname(FixtureClass::class, false)],
[]
);

return new IsReadonlyRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
self::createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}

0 comments on commit cb3b200

Please sign in to comment.