Skip to content

Commit

Permalink
Add override method attributes (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Nov 27, 2023
1 parent aa92f8e commit a489680
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Install PHP dependencies
run: |
if [ "${{ matrix.type }}" != "Phpunit" ] && [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpunit/phpunit atk4/ergebnis-phpunit-slow-test-detector --dev; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer ergebnis/composer-normalize --dev; fi
if [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpstan/\* --dev; fi
composer update --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
- php: 'latest'
type: 'Phpunit Burn'
env:
LOG_COVERAGE: "${{ fromJSON('{true: \"1\", false: \"\"}')[matrix.php == '8.2' && matrix.type == 'Phpunit' && (github.event_name == 'pull_request' || (github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master')))] }}"
LOG_COVERAGE: "${{ fromJSON('{true: \"1\", false: \"\"}')[matrix.php == '8.3' && matrix.type == 'Phpunit' && (github.event_name == 'pull_request' || (github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master')))] }}"
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -109,12 +109,12 @@ jobs:
- name: Install PHP dependencies
run: |
if [ "${{ matrix.type }}" != "Phpunit" ] && [ "${{ matrix.type }}" != "Phpunit Lowest" ] && [ "${{ matrix.type }}" != "Phpunit Burn" ]; then composer remove --no-interaction --no-update phpunit/phpunit atk4/ergebnis-phpunit-slow-test-detector --dev; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer ergebnis/composer-normalize --dev; fi
if [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpstan/\* --dev; fi
if [ -n "$LOG_COVERAGE" ]; then composer require --no-interaction --no-install phpunit/phpcov; fi
composer update --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader
if [ "${{ matrix.type }}" = "Phpunit Lowest" ]; then composer update --ansi --prefer-dist --prefer-lowest --prefer-stable --no-interaction --no-progress --optimize-autoloader; fi
if [ "${{ matrix.type }}" = "Phpunit Burn" ]; then sed -i 's~public function runBare(): void~public function runBare(): void { gc_collect_cycles(); gc_collect_cycles(); $memDiffs = array_fill(0, '"$(if [ \"$GITHUB_EVENT_NAME\" == \"schedule\" ]; then echo 1024; else echo 64; fi)"', 0); for ($i = -1; $i < count($memDiffs); ++$i) { $this->_runBare(); gc_collect_cycles(); gc_collect_cycles(); $mem = memory_get_usage(); if ($i !== -1) { $memDiffs[$i] = $mem - $memPrev; } $memPrev = $mem; rsort($memDiffs); if (array_sum($memDiffs) >= 4096 * 1024 || $memDiffs[2] > 0) { $this->onNotSuccessfulTest(new AssertionFailedError("Memory leak detected! (" . implode(" + ", array_map(static fn ($v) => number_format($v / 1024, 3, ".", " "), array_filter($memDiffs))) . " KB, " . ($i + 2) . " iterations)")); } } } private function _runBare(): void~' vendor/phpunit/phpunit/src/Framework/TestCase.php && cat vendor/phpunit/phpunit/src/Framework/TestCase.php | grep '_runBare('; fi
if [ "${{ matrix.type }}" = "Phpunit Burn" ]; then sed -i 's~public function runBare(): void~public function runBare(): void { gc_collect_cycles(); $memDiffs = array_fill(0, '"$(if [ \"$GITHUB_EVENT_NAME\" == \"schedule\" ]; then echo 1024; else echo 64; fi)"', 0); for ($i = -1; $i < count($memDiffs); ++$i) { $this->_runBare(); gc_collect_cycles(); $mem = memory_get_usage(); if ($i !== -1) { $memDiffs[$i] = $mem - $memPrev; } $memPrev = $mem; rsort($memDiffs); if (array_sum($memDiffs) >= 4096 * 1024 || $memDiffs[2] > 0) { $e = new AssertionFailedError("Memory leak detected! (" . implode(" + ", array_map(static fn ($v) => number_format($v / 1024, 3, ".", " "), array_filter($memDiffs))) . " KB, " . ($i + 2) . " iterations)"); $this->status = TestStatus::failure($e->getMessage()); Event\\Facade::emitter()->testFailed($this->valueObjectForEvents(), Event\\Code\\ThrowableBuilder::from($e), Event\\Code\\ComparisonFailureBuilder::from($e)); $this->onNotSuccessfulTest($e); } } } private function _runBare(): void~' vendor/phpunit/phpunit/src/Framework/TestCase.php && cat vendor/phpunit/phpunit/src/Framework/TestCase.php | grep '_runBare('; fi
- name: Init
run: |
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ parameters:
excludePaths:
- vendor

checkMissingOverrideMethodAttribute: true

ignoreErrors:
# relax strict rules
- '~^Only booleans are allowed in .+, .+ given( on the (left|right) side)?\.~'
Expand Down Expand Up @@ -46,3 +48,9 @@ parameters:
path: 'tests/Phpunit/TestCaseTest.php'
message: '~^Access to constant STATUS_INCOMPLETE on an unknown class PHPUnit\\Runner\\BaseTestRunner\.$~'
count: 1

# https://github.com/phpstan/phpstan/issues/10150
-
path: 'src/DebugTrait.php'
message: '~^Method Atk4\\Core\\Tests\\DebugPsrMock::\w+\(\) overrides method Psr\\Log\\LoggerInterface::\w+\(\) but is missing the #\[\\Override\] attribute\.$~'
count: 9
1 change: 1 addition & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function setMessage(string $message): self
return $this;
}

#[\Override]
public function toString(): string
{
$res = static::class . ': ' . $this->getMessage() . "\n";
Expand Down
6 changes: 6 additions & 0 deletions src/ExceptionRenderer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Console extends RendererAbstract
{
#[\Override]
protected function processHeader(): void
{
$title = $this->getExceptionTitle();
Expand All @@ -26,6 +27,7 @@ protected function processHeader(): void
EOF, $tokens);
}

#[\Override]
protected function processParams(): void
{
if (!$this->exception instanceof Exception) {
Expand All @@ -45,6 +47,7 @@ protected function processParams(): void
}
}

#[\Override]
protected function processSolutions(): void
{
if (!$this->exception instanceof Exception) {
Expand All @@ -60,6 +63,7 @@ protected function processSolutions(): void
}
}

#[\Override]
protected function processStackTrace(): void
{
$this->output .= <<<'EOF'
Expand All @@ -70,6 +74,7 @@ protected function processStackTrace(): void
$this->processStackTraceInternal();
}
#[\Override]
protected function processStackTraceInternal(): void
{
$text = <<<'EOF'
Expand Down Expand Up @@ -120,6 +125,7 @@ protected function processStackTraceInternal(): void
}
}
#[\Override]
protected function processPreviousException(): void
{
if (!$this->exception->getPrevious()) {
Expand Down
6 changes: 6 additions & 0 deletions src/ExceptionRenderer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Html extends RendererAbstract
{
#[\Override]
protected function processHeader(): void
{
$title = $this->getExceptionTitle();
Expand Down Expand Up @@ -37,6 +38,7 @@ protected function encodeHtml(string $value): string
return htmlspecialchars($value, \ENT_HTML5 | \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
}

#[\Override]
protected function processParams(): void
{
if (!$this->exception instanceof Exception) {
Expand Down Expand Up @@ -73,6 +75,7 @@ protected function processParams(): void
$this->output .= $this->replaceTokens($text, $tokens);
}

#[\Override]
protected function processSolutions(): void
{
if (!$this->exception instanceof Exception) {
Expand Down Expand Up @@ -106,6 +109,7 @@ protected function processSolutions(): void
$this->output .= $this->replaceTokens($text, $tokens);
}

#[\Override]
protected function processStackTrace(): void
{
$this->output .= '
Expand All @@ -123,6 +127,7 @@ protected function processStackTrace(): void
';
}

#[\Override]
protected function processStackTraceInternal(): void
{
$text = '
Expand Down Expand Up @@ -184,6 +189,7 @@ protected function processStackTraceInternal(): void
}
}

#[\Override]
protected function processPreviousException(): void
{
if (!$this->exception->getPrevious()) {
Expand Down
8 changes: 8 additions & 0 deletions src/ExceptionRenderer/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Json extends RendererAbstract
'previous' => [],
];

#[\Override]
protected function processHeader(): void
{
$title = $this->getExceptionTitle();
Expand All @@ -32,6 +33,7 @@ protected function processHeader(): void
$this->json['class'] = $class;
}

#[\Override]
protected function processParams(): void
{
if (!$this->exception instanceof Exception) {
Expand All @@ -47,6 +49,7 @@ protected function processParams(): void
}
}

#[\Override]
protected function processSolutions(): void
{
if (!$this->exception instanceof Exception) {
Expand All @@ -65,13 +68,15 @@ protected function processSolutions(): void
}
}

#[\Override]
protected function processStackTrace(): void
{
$this->output .= '<span style="color: sandybrown;">Stack Trace:</span>' . "\n";

$this->processStackTraceInternal();
}

#[\Override]
protected function processStackTraceInternal(): void
{
$inAtk = true;
Expand All @@ -95,6 +100,7 @@ protected function processStackTraceInternal(): void
}
}

#[\Override]
protected function processPreviousException(): void
{
if (!$this->exception->getPrevious()) {
Expand All @@ -107,6 +113,7 @@ protected function processPreviousException(): void
$this->json['previous'] = $previous->json;
}

#[\Override]
protected function parseStackTraceFrame(array $frame): array
{
return [
Expand All @@ -119,6 +126,7 @@ protected function parseStackTraceFrame(array $frame): array
];
}

#[\Override]
public function __toString(): string
{
try {
Expand Down
1 change: 1 addition & 0 deletions src/ExceptionRenderer/RendererAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function processAll(): void
$this->processPreviousException();
}

#[\Override]
public function __toString(): string
{
try {
Expand Down
4 changes: 4 additions & 0 deletions src/Phpunit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
if (\PHP_VERSION_ID >= 8_01_00) {
trait Phpunit9xTestCaseTrait
{
#[\Override]
protected function onNotSuccessfulTest(\Throwable $e): never
{
$this->_onNotSuccessfulTest($e);
Expand All @@ -24,6 +25,7 @@ protected function onNotSuccessfulTest(\Throwable $e): never
} else {
trait Phpunit9xTestCaseTrait
{
#[\Override]
protected function onNotSuccessfulTest(\Throwable $e): void
{
$this->_onNotSuccessfulTest($e);
Expand All @@ -44,6 +46,7 @@ final public static function isPhpunit9x(): bool
return (new \ReflectionClass(self::class))->hasMethod('getStatus');
}

#[\Override]
protected function setUp(): void
{
// rerun data providers to fix coverage when coverage for test files is enabled
Expand Down Expand Up @@ -76,6 +79,7 @@ protected function setUp(): void
parent::setUp();
}

#[\Override]
protected function tearDown(): void
{
parent::tearDown();
Expand Down
4 changes: 1 addition & 3 deletions src/Translator/Adapter/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class Generic implements ITranslatorAdapter
/** @var array<string, array<string, array<string, non-empty-array<string, string>>>> */
protected array $definitions = [];

/**
* @param array<string, mixed> $parameters
*/
#[\Override]
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string
{
$definition = $this->getDefinition($message, $domain ?? 'atk', $locale ?? 'en');
Expand Down
1 change: 1 addition & 0 deletions tests/DebugTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class DebugAppMock implements \Psr\Log\LoggerInterface
/** @var self */
public $logger;

#[\Override]
public function log($level, $message, array $context = []): void
{
$this->log = [$level, $message, $context];
Expand Down
1 change: 1 addition & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class TrackableMock2
class ExceptionTestThrowError extends Exception
{
#[\Override]
public function getCustomExceptionTitle(): string
{
throw new \Exception('just to cover __string');
Expand Down
3 changes: 3 additions & 0 deletions tests/InitializerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testInitNotCalled(): void
public function testInitNoParentCalledException(): void
{
$m = new class() extends AbstractInitializerMock {
#[\Override]
protected function init(): void {}
};

Expand All @@ -69,6 +70,7 @@ public function testInitCalledTwiceException(): void
public function testInitDeclaredPublicException(): void
{
$m = new class() extends AbstractInitializerMock {
#[\Override]
public function init(): void {}
};

Expand All @@ -88,6 +90,7 @@ class InitializerMock extends AbstractInitializerMock
/** @var bool */
public $result = false;

#[\Override]
protected function init(): void
{
parent::init();
Expand Down
1 change: 1 addition & 0 deletions tests/Translator/AdapterAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class AdapterAppTest extends AdapterTestCase
{
#[\Override]
public function getTranslatableMock(): object
{
$app = new class() {
Expand Down
1 change: 1 addition & 0 deletions tests/Translator/AdapterGenericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class AdapterGenericTest extends AdapterTestCase
{
#[\Override]
public function getTranslatableMock(): object
{
return new class() {
Expand Down
1 change: 1 addition & 0 deletions tests/Translator/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class AdapterTestCase extends TestCase
{
abstract public function getTranslatableMock(): object;

#[\Override]
protected function setUp(): void
{
parent::setUp();
Expand Down

0 comments on commit a489680

Please sign in to comment.