-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix readonly final class architecture side effect issue PHP8.2 (#645)
* fix readonly final class architecture side effect issue PHP8.2 * add readonly also for abstract * add tests * bump min codesniffer for T_READONLY * tests v2 * test again * update phpunit config --------- Co-authored-by: Marvin Courcier <courciermarvin@gmail.com> Co-authored-by: Chris Gmyr <cmgmyr@gmail.com>
- Loading branch information
1 parent
a71a20b
commit 382f294
Showing
7 changed files
with
93 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
.idea/* | ||
.env | ||
.phpunit.result.cache | ||
.phpunit.cache/ | ||
/composer.lock | ||
.php_cs.cache | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Tests\Domain\Insights\Fixtures; | ||
|
||
/** | ||
* @see \Tests\Domain\Insights\ReadonlyClassTest | ||
*/ | ||
readonly class ReadonlyClass { | ||
public function __construct( | ||
private string $foo, | ||
private string $bar, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
$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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters