From 492dab943ea0bbb2829a3cd9cc8b9293b5223bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 16 Dec 2023 13:01:13 +0100 Subject: [PATCH] Fix phpstan v1.10.50 (#2135) --- docs/view.md | 4 ++-- phpstan.neon.dist | 10 ---------- src/App.php | 35 +++++++++++++++++------------------ src/Behat/MinkSession.php | 2 +- tests/DemosTest.php | 18 ------------------ 5 files changed, 20 insertions(+), 49 deletions(-) diff --git a/docs/view.md b/docs/view.md index a3362105e5..9729b5c3b6 100644 --- a/docs/view.md +++ b/docs/view.md @@ -130,10 +130,10 @@ Injection. Consider the following example: ``` -$app->debug = new Logger('log'); // Monolog +$app->logger = new Logger('log'); // Monolog // next, somewhere in a render tree -$view->getApp()->debug->log('Foo Bar'); +$view->getApp()->logger->log('Foo Bar'); ``` Agile UI will automatically pass your $app class to all the views. diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e18cdc69d3..8ff5c6b53b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -18,16 +18,6 @@ parameters: - '~^Only booleans are allowed in .+, .+ given( on the (left|right) side)?\.~' - '~^Variable (static )?(property access|method call) on .+\.~' - # remove once PHPUnit 9.x support is removed - - - path: 'tests/DemosTest.php' - message: '~^Access to constant (STATUS_PASSED|STATUS_INCOMPLETE|STATUS_SKIPPED) on an unknown class PHPUnit\\Runner\\BaseTestRunner\.$~' - count: 3 - - - path: 'tests/DemosTest.php' - message: '~^Call to an undefined method Atk4\\Ui\\Tests\\DemosTest::(getName|getStatus)\(\)\.$~' - count: 4 - # TODO these rules are generated, this ignores should be fixed in the code # for level = 2 - diff --git a/src/App.php b/src/App.php index 5dda75d0bb..d65fa8d824 100644 --- a/src/App.php +++ b/src/App.php @@ -36,6 +36,8 @@ class App use DynamicMethodTrait; use HookTrait; + private const UNSUPPRESSEABLE_ERROR_LEVELS = \PHP_MAJOR_VERSION >= 8 ? (\E_ERROR | \E_PARSE | \E_CORE_ERROR | \E_COMPILE_ERROR | \E_USER_ERROR | \E_RECOVERABLE_ERROR) : 0; + public const HOOK_BEFORE_EXIT = self::class . '@beforeExit'; public const HOOK_BEFORE_RENDER = self::class . '@beforeRender'; @@ -182,7 +184,7 @@ public function __construct(array $defaults = []) if ($this->catchExceptions) { set_exception_handler(\Closure::fromCallable([$this, 'caughtException'])); set_error_handler(static function (int $severity, string $msg, string $file, int $line): bool { - if ((error_reporting() & ~(\PHP_MAJOR_VERSION >= 8 ? (\E_ERROR | \E_PARSE | \E_CORE_ERROR | \E_COMPILE_ERROR | \E_USER_ERROR | \E_RECOVERABLE_ERROR) : 0)) === 0) { + if ((error_reporting() & ~self::UNSUPPRESSEABLE_ERROR_LEVELS) === 0) { $isFirstFrame = true; foreach (array_slice(debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 10), 1) as $frame) { // allow to suppress any warning outside Atk4 @@ -281,7 +283,7 @@ public function callExit(): void exit; } - public function caughtException(\Throwable $exception): void + protected function caughtException(\Throwable $exception): void { if ($exception instanceof LateOutputError) { $this->outputLateOutputError($exception); @@ -1105,24 +1107,21 @@ public function renderExceptionHtml(\Throwable $exception): string protected function setupAlwaysRun(): void { - register_shutdown_function( - function () { - if (!$this->runCalled) { - try { - $this->run(); - } catch (ExitApplicationError $e) { - // let the process go and stop on ->callExit below - } catch (\Throwable $e) { - // set_exception_handler does not work in shutdown - // https://github.com/php/php-src/issues/10695 - $this->caughtException($e); - } - - // call with true to trigger beforeExit event - $this->callBeforeExit(); + register_shutdown_function(function () { + if (!$this->runCalled) { + try { + $this->run(); + } catch (ExitApplicationError $e) { + // let the process continue and terminate using self::callExit() below + } catch (\Throwable $e) { + // set_exception_handler does not work in shutdown + // https://github.com/php/php-src/issues/10695 + $this->caughtException($e); } + + $this->callBeforeExit(); } - ); + }); } /** diff --git a/src/Behat/MinkSession.php b/src/Behat/MinkSession.php index a31f102263..228002ecae 100644 --- a/src/Behat/MinkSession.php +++ b/src/Behat/MinkSession.php @@ -15,7 +15,7 @@ public function __construct(\Behat\Mink\Session $session) { $driver = new MinkSeleniumDriver($session->getDriver()); // @phpstan-ignore-line - parent::__construct($driver, $session->getSelectorsHandler()); + parent::__construct($driver, $session->getSelectorsHandler()); // @phpstan-ignore-line } #[\Override] diff --git a/tests/DemosTest.php b/tests/DemosTest.php index a23e3ba0a1..b99d94d325 100644 --- a/tests/DemosTest.php +++ b/tests/DemosTest.php @@ -14,7 +14,6 @@ use Atk4\Ui\Layout; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; -use PHPUnit\Runner\BaseTestRunner; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -32,9 +31,6 @@ class DemosTest extends TestCase private static ?Persistence $_db = null; - /** @var array */ - private static array $_failedParentTests = []; - #[\Override] public static function setUpBeforeClass(): void { @@ -77,20 +73,6 @@ protected function setUp(): void } } - #[\Override] - protected function _onNotSuccessfulTest(\Throwable $t): void - { - if (self::isPhpunit9x() ? !in_array($this->getStatus(), [BaseTestRunner::STATUS_PASSED, BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE], true) : !$this->status()->isSuccess() && !$this->status()->isSkipped() && !$this->status()->isIncomplete()) { - if (!isset(self::$_failedParentTests[self::isPhpunit9x() ? $this->getName() : $this->nameWithDataSet()])) { - self::$_failedParentTests[self::isPhpunit9x() ? $this->getName() : $this->nameWithDataSet()] = self::isPhpunit9x() ? $this->getStatus() : $this->status()->asInt(); - } else { - self::markTestIncomplete('Test failed, but non-HTTP test failed too, fix it first'); - } - } - - throw $t; - } - protected function setSuperglobalsFromRequest(RequestInterface $request): void { $this->resetSuperglobals();