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

fix readonly final class architecture side effect issue PHP8.2 #645

Merged
merged 7 commits into from
Sep 28, 2023
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
.idea/*
.env
.phpunit.result.cache
.phpunit.cache/
/composer.lock
.php_cs.cache
.php_cs.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"psr/simple-cache": "^1.0|^2.0|^3.0",
"sebastian/diff": "^4.0|^5.0",
"slevomat/coding-standard": "^7.0.8|^8.0",
"squizlabs/php_codesniffer": "^3.5",
"squizlabs/php_codesniffer": "^3.7",
"symfony/cache": "^4.4|^5.0|^6.0",
"symfony/console": "^4.2.12|^5.0|^6.0",
"symfony/finder": "^4.2.12|^5.0|^6.0",
Expand Down
13 changes: 7 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/squizlabs/php_codesniffer/tests/bootstrap.php" colors="true" failOnRisky="true" failOnWarning="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/squizlabs/php_codesniffer/tests/bootstrap.php" colors="true" failOnRisky="true" failOnWarning="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Domain/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
/**
* Processes a single file.
*/
private function analyseFile(Collector $collector, string $filename): void

Check failure on line 72 in src/Domain/Analyser.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Function length] Your function is too long. Currently using 244 lines. Can be up to 20 lines.
{
$buffer = (string) \file_get_contents($filename);
$tokens = @\token_get_all($buffer);
Expand Down Expand Up @@ -177,9 +177,9 @@
$collector->incrementInterfaces();
} elseif (isset($tokens[$i - 2]) &&
\is_array($tokens[$i - 2])) {
if ($tokens[$i - 2][0] === \T_ABSTRACT) {
if ($tokens[$i - 2][0] === \T_ABSTRACT || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_ABSTRACT) {

Check failure on line 180 in src/Domain/Analyser.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Line length] Line exceeds maximum limit of 120 characters; contains 141 characters
$collector->addAbstractClass($filename);
} elseif ($tokens[$i - 2][0] === \T_FINAL) {
} elseif ($tokens[$i - 2][0] === \T_FINAL || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_FINAL) {

Check failure on line 182 in src/Domain/Analyser.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Line length] Line exceeds maximum limit of 120 characters; contains 141 characters
$collector->addConcreteFinalClass($filename);
} else {
$collector->addConcreteNonFinalClass($filename);
Expand Down
14 changes: 14 additions & 0 deletions tests/Domain/Insights/Fixtures/ReadonlyClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

Check failure on line 1 in tests/Domain/Insights/Fixtures/ReadonlyClass.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Normal classes are forbidden. Classes must be final or abstract]

namespace Tests\Domain\Insights\Fixtures;

/**
* @see \Tests\Domain\Insights\ReadonlyClassTest
*/
readonly class ReadonlyClass {
public function __construct(
private string $foo,
private string $bar,
) {
}
}
64 changes: 64 additions & 0 deletions tests/Domain/Insights/ReadonlyClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Tests\Domain\Insights;

use NunoMaduro\PhpInsights\Application\Console\Formatters\PathShortener;
use NunoMaduro\PhpInsights\Domain\Analyser;
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses;
use NunoMaduro\PhpInsights\Domain\Metrics\Architecture\Classes;
use Tests\TestCase;

final class ReadonlyClassTest extends TestCase
{
public function testHasIssue(): void
{
if (PHP_VERSION_ID < 80200) {
self::markTestSkipped('Readonly classes are only available in PHP 8.2+');
}

$files = [
__DIR__ . '/Fixtures/ReadonlyClass.php',
];

$analyzer = new Analyser();
$collector = $analyzer->analyse([__DIR__ . '/Fixtures/'], $files, PathShortener::extractCommonPath($files));
$insight = new ForbiddenNormalClasses($collector, []);

self::assertTrue($insight->hasIssue());
self::assertIsArray($insight->getDetails());
self::assertNotEmpty($insight->getDetails());
}

public function testSkipFile(): void

Check failure on line 34 in tests/Domain/Insights/ReadonlyClassTest.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Function length] Your function is too long. Currently using 23 lines. Can be up to 20 lines.
{
$fileLocation = __DIR__ . '/Fixtures/ReadonlyClass.php';

$collection = $this->runAnalyserOnConfig(
[
'add' => [
Classes::class => [
ForbiddenNormalClasses::class,
],
],
'config' => [
ForbiddenNormalClasses::class => [
'exclude' => [$fileLocation],
],
],
],
[$fileLocation]
);

$classErrors = 0;

foreach ($collection->allFrom(new Classes()) as $insight) {
if ($insight->hasIssue() && $insight->getInsightClass() === ForbiddenNormalClasses::class) {
$classErrors++;
}
}

self::assertEquals(0, $classErrors);
}
}
6 changes: 3 additions & 3 deletions tests/Domain/Insights/SyntaxCheckTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in tests/Domain/Insights/SyntaxCheckTest.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Having `classes` with more than 5 cyclomatic complexity is prohibited - Consider refactoring] 9 cyclomatic complexity

declare(strict_types=1);

Expand Down Expand Up @@ -32,7 +32,7 @@
}
}

public function testHasIssueOnDirectory(): void

Check failure on line 35 in tests/Domain/Insights/SyntaxCheckTest.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Function length] Your function is too long. Currently using 22 lines. Can be up to 20 lines.
{
$basepath = __DIR__ . '/Fixtures/InvalidPhpCode';

Expand All @@ -49,17 +49,17 @@
usort($details, static fn (Details $a, Details $b): int => $a->getFile() <=> $b->getFile());

self::assertTrue($insight->hasIssue());
if (PHP_MAJOR_VERSION === 7) {
self::assertCount(2, $details);
} else {
if (PHP_MAJOR_VERSION === 7 || PHP_VERSION_ID >= 80200) {
self::assertCount(3, $details);
} else {
self::assertCount(4, $details);
}

/** @var \NunoMaduro\PhpInsights\Domain\Details $detail */
$detail = $details[0];
self::assertStringContainsString('PHP syntax error: syntax error, unexpected', $detail->getMessage());
self::assertSame(2, $detail->getLine());
self::assertStringContainsString('Fixtures/InvalidPhpCode/SemiColonAfterExtendClass.php', $detail->getFile());

Check failure on line 62 in tests/Domain/Insights/SyntaxCheckTest.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Line length] Line exceeds maximum limit of 120 characters; contains 126 characters
}
}
}
Expand All @@ -80,7 +80,7 @@
self::assertTrue($insight->hasIssue());
$detail = $insight->getDetails()[0];
self::assertStringNotContainsString('Fixtures/InvalidPhpCode/_ide_helper.php', $detail->getFile());
self::assertStringNotContainsString('PHP syntax error: Cannot use League\\Container', $detail->getMessage());

Check failure on line 83 in tests/Domain/Insights/SyntaxCheckTest.php

View workflow job for this annotation

GitHub Actions / 7.4 - prefer-stable

* [Line length] Line exceeds maximum limit of 120 characters; contains 125 characters
}
}
}
Expand Down
Loading