From 6eb4c213b9c77d306622ea11e444d37fac28c694 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 16 Aug 2022 11:08:53 +0200 Subject: [PATCH] cleanup code --- .../Models/Collectors/OxidConfigCollector.php | 9 +++- .../Models/Collectors/OxidShopCollector.php | 28 ++++++++++--- .../Collectors/OxidVersionCollector.php | 24 +++++------ .../Models/Collectors/SmartyCollector.php | 8 ++-- Application/Models/DebugBarHandler.php | 29 +++++++------ .../Exceptions/CompileErrorException.php | 2 +- .../Models/Exceptions/CoreErrorException.php | 2 +- .../Models/Exceptions/ParseException.php | 2 +- .../Models/Exceptions/UserErrorException.php | 2 +- Core/DebugBarErrorHandler.php | 41 +++++++++++-------- Core/DebugBarExceptionHandler.php | 18 ++++---- Modules/Core/Config_DebugBar.php | 2 +- Modules/Core/ShopControl_DebugBar.php | 7 ++-- Modules/functions.php | 3 +- Setup/Events.php | 27 +++++------- phpstan.neon | 5 +++ 16 files changed, 118 insertions(+), 91 deletions(-) diff --git a/Application/Models/Collectors/OxidConfigCollector.php b/Application/Models/Collectors/OxidConfigCollector.php index 320deff..7dd36aa 100644 --- a/Application/Models/Collectors/OxidConfigCollector.php +++ b/Application/Models/Collectors/OxidConfigCollector.php @@ -36,6 +36,11 @@ class OxidConfigCollector extends DataCollector implements Renderable */ protected $useHtmlVarDumper = false; + /** + * @param Config $config + * + * @throws ReflectionException + */ public function __construct(Config $config) { $config->init(); @@ -111,9 +116,9 @@ class OxidConfigCollector extends DataCollector implements Renderable * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable * rendering. * - * @return mixed + * @return bool */ - public function isHtmlVarDumperUsed() + public function isHtmlVarDumperUsed(): bool { return $this->useHtmlVarDumper; } diff --git a/Application/Models/Collectors/OxidShopCollector.php b/Application/Models/Collectors/OxidShopCollector.php index d7f7654..5c66a7d 100644 --- a/Application/Models/Collectors/OxidShopCollector.php +++ b/Application/Models/Collectors/OxidShopCollector.php @@ -18,13 +18,17 @@ namespace D3\DebugBar\Application\Models\Collectors; use Composer\InstalledVersions; use DebugBar\DataCollector\DataCollector; use DebugBar\DataCollector\Renderable; +use Exception; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Module\Module; use OxidEsales\Eshop\Core\ShopVersion; use OxidEsales\Eshop\Core\Theme; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridge; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface; use OxidEsales\Facts\Facts; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class OxidShopCollector extends DataCollector implements Renderable { @@ -39,11 +43,18 @@ class OxidShopCollector extends DataCollector implements Renderable */ protected $useHtmlVarDumper = true; + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception + */ public function __construct() { $facts = new Facts(); $theme = new Theme(); - $parentThemeId = $theme->getParent() ? $theme->getParent()->getId() : '--'; + /** @var Theme|null $parent */ + $parent = $theme->getParent(); + $parentThemeId = $parent ? $parent->getId() : '--'; $moduleList = $this->getInstalledModules(); array_walk( @@ -60,7 +71,7 @@ class OxidShopCollector extends DataCollector implements Renderable 'CE Version:' => InstalledVersions::getVersion('oxid-esales/oxideshop-ce'), 'Theme:' => $theme->getActiveThemeId(), 'Parent Theme:' => $parentThemeId, - 'Modules:' => implode(chr(10), $moduleList) + 'Modules:' => implode(chr(10), $moduleList), ]; } @@ -96,9 +107,9 @@ class OxidShopCollector extends DataCollector implements Renderable * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable * rendering. * - * @return mixed + * @return bool */ - public function isHtmlVarDumperUsed() + public function isHtmlVarDumperUsed(): bool { return $this->useHtmlVarDumper; } @@ -121,10 +132,17 @@ class OxidShopCollector extends DataCollector implements Renderable ]; } + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ protected function getInstalledModules(): array { $container = ContainerFactory::getInstance()->getContainer(); - $shopConfiguration = $container->get(ShopConfigurationDaoBridgeInterface::class)->get(); + /** @var ShopConfigurationDaoBridge $shopConfigurationDaoBridge */ + $shopConfigurationDaoBridge = $container->get(ShopConfigurationDaoBridgeInterface::class); + $shopConfiguration = $shopConfigurationDaoBridge->get(); $modules = []; diff --git a/Application/Models/Collectors/OxidVersionCollector.php b/Application/Models/Collectors/OxidVersionCollector.php index c14f4d1..4175720 100644 --- a/Application/Models/Collectors/OxidVersionCollector.php +++ b/Application/Models/Collectors/OxidVersionCollector.php @@ -1,4 +1,5 @@ getParent() ? $theme->getParent()->getId() : '--'; - return array( - 'version' => $facts->getEdition().' '.ShopVersion::getVersion() - ); + return [ + 'version' => $facts->getEdition().' '.ShopVersion::getVersion(), + ]; } /** - * {@inheritDoc} + * @return string[][] */ - public function getWidgets() + public function getWidgets(): array { - $collect = $this->collect(); - return [ "oxidversion" => [ "icon" => "shopping-cart", "tooltip" => 'OXID Version', "map" => $this->getName().".version", - "default" => "" + "default" => "", ], ]; } diff --git a/Application/Models/Collectors/SmartyCollector.php b/Application/Models/Collectors/SmartyCollector.php index 5af0e78..b223159 100644 --- a/Application/Models/Collectors/SmartyCollector.php +++ b/Application/Models/Collectors/SmartyCollector.php @@ -38,7 +38,7 @@ class SmartyCollector extends DataCollector implements Renderable * @param bool $value * @return $this */ - public function useHtmlVarDumper($value = true) + public function useHtmlVarDumper(bool $value = true): SmartyCollector { $this->useHtmlVarDumper = $value; @@ -49,9 +49,9 @@ class SmartyCollector extends DataCollector implements Renderable * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable * rendering. * - * @return mixed + * @return bool */ - public function isHtmlVarDumperUsed() + public function isHtmlVarDumperUsed(): bool { return $this->useHtmlVarDumper; } @@ -69,7 +69,7 @@ class SmartyCollector extends DataCollector implements Renderable */ public function collect(): array { - $data = []; + $data = ['current view template' => Registry::getConfig()->getTopActiveView()->getTemplateName()]; $vars = $this->smarty->get_template_vars(); diff --git a/Application/Models/DebugBarHandler.php b/Application/Models/DebugBarHandler.php index 3a1ee7c..0040e3a 100644 --- a/Application/Models/DebugBarHandler.php +++ b/Application/Models/DebugBarHandler.php @@ -28,13 +28,12 @@ class DebugBarHandler public function setErrorHandler(): void { if ($this->d3CanActivateDebugBar()) { - set_error_handler( - [ - new DebugBarErrorHandler(), - 'callback' - ], - $this->getHandledErrorTypes() - ); + /** @var callable $callable */ + $callable = [ + new DebugBarErrorHandler(), + 'callback', + ]; + set_error_handler($callable, $this->getHandledErrorTypes()); } } @@ -52,10 +51,10 @@ class DebugBarHandler public function setExceptionHandler(): void { if ($this->d3CanActivateDebugBar()) { - set_exception_handler( [ + set_exception_handler([ new DebugBarExceptionHandler(), - 'handleUncaughtException' - ] ); + 'handleUncaughtException', + ]); } } @@ -65,17 +64,17 @@ class DebugBarHandler public function addDebugBarComponent(): void { if ($this->d3CanActivateDebugBar()) { - $userComponentNames = Registry::getConfig()->getConfigParam( 'aUserComponentNames' ); + $userComponentNames = Registry::getConfig()->getConfigParam('aUserComponentNames'); $d3CmpName = DebugBarComponent::class; $blDontUseCache = 1; - if ( ! is_array( $userComponentNames ) ) { + if (! is_array($userComponentNames)) { $userComponentNames = []; } - if ( ! in_array( $d3CmpName, array_keys( $userComponentNames ) ) ) { + if (! in_array($d3CmpName, array_keys($userComponentNames))) { $userComponentNames[ $d3CmpName ] = $blDontUseCache; - Registry::getConfig()->setConfigParam( 'aUserComponentNames', $userComponentNames ); + Registry::getConfig()->setConfigParam('aUserComponentNames', $userComponentNames); } } } @@ -87,4 +86,4 @@ class DebugBarHandler { return false === isAdmin(); } -} \ No newline at end of file +} diff --git a/Application/Models/Exceptions/CompileErrorException.php b/Application/Models/Exceptions/CompileErrorException.php index 94e0d28..b8bd340 100644 --- a/Application/Models/Exceptions/CompileErrorException.php +++ b/Application/Models/Exceptions/CompileErrorException.php @@ -19,4 +19,4 @@ use ErrorException; class CompileErrorException extends ErrorException { -} \ No newline at end of file +} diff --git a/Application/Models/Exceptions/CoreErrorException.php b/Application/Models/Exceptions/CoreErrorException.php index ef94370..d087f59 100644 --- a/Application/Models/Exceptions/CoreErrorException.php +++ b/Application/Models/Exceptions/CoreErrorException.php @@ -19,4 +19,4 @@ use ErrorException; class CoreErrorException extends ErrorException { -} \ No newline at end of file +} diff --git a/Application/Models/Exceptions/ParseException.php b/Application/Models/Exceptions/ParseException.php index 28a6ca8..ffe4577 100644 --- a/Application/Models/Exceptions/ParseException.php +++ b/Application/Models/Exceptions/ParseException.php @@ -19,4 +19,4 @@ use ErrorException; class ParseException extends ErrorException { -} \ No newline at end of file +} diff --git a/Application/Models/Exceptions/UserErrorException.php b/Application/Models/Exceptions/UserErrorException.php index d4e62a8..b34d933 100644 --- a/Application/Models/Exceptions/UserErrorException.php +++ b/Application/Models/Exceptions/UserErrorException.php @@ -19,4 +19,4 @@ use ErrorException; class UserErrorException extends ErrorException { -} \ No newline at end of file +} diff --git a/Core/DebugBarErrorHandler.php b/Core/DebugBarErrorHandler.php index dd93849..1427f61 100644 --- a/Core/DebugBarErrorHandler.php +++ b/Core/DebugBarErrorHandler.php @@ -25,10 +25,10 @@ use OxidEsales\Eshop\Core\Registry; class DebugBarErrorHandler { /** - * @param $severity - * @param $message - * @param $file - * @param $line + * @param int $severity + * @param string $message + * @param string $file + * @param int $line * * @return void|false * @throws CompileErrorException @@ -37,22 +37,22 @@ class DebugBarErrorHandler * @throws ParseException * @throws UserErrorException */ - public function callback( $severity, $message, $file, $line ) + public function callback(int $severity, string $message, string $file, int $line) { global $debugBarErrorOccured; $debugBarErrorOccured = 1; - if ( 0 === error_reporting() || !( error_reporting() & $severity ) ) { + if (0 === error_reporting() || !(error_reporting() & $severity)) { // This error code is not included in error_reporting. return false; } - $smartyTemplate = $this->getSmartyTemplateLocationFromError( $message ); - if ( is_array( $smartyTemplate ) ) { + $smartyTemplate = $this->getSmartyTemplateLocationFromError($message); + if (is_array($smartyTemplate)) { [ $file, $line ] = $smartyTemplate; } - switch($severity) { + switch ($severity) { case E_CORE_ERROR: throw new CoreErrorException($message, 0, $severity, $file, $line); case E_COMPILE_ERROR: @@ -69,10 +69,10 @@ class DebugBarErrorHandler } /** - * @param $messsage + * @param string $messsage * @return array|null */ - protected function getSmartyTemplateLocationFromError($messsage): ?array + protected function getSmartyTemplateLocationFromError(string $messsage): ?array { if (stristr($messsage, 'Smarty error: [in ')) { $start = strpos($messsage, '[')+1; @@ -85,15 +85,20 @@ class DebugBarErrorHandler } /** - * @param string $message - * @param int|null $severity - * @param string|null $file - * @param int|null $line + * @param string $message + * @param int $severity + * @param string $file + * @param int $line + * @return void * * @throws ErrorException */ - protected function handleUnregisteredErrorTypes(string $message = '', int $severity = null, string $file = null, int $line = null) - { + protected function handleUnregisteredErrorTypes( + string $message = '', + int $severity = 1, + string $file = __FILE__, + int $line = __LINE__ + ): void { throw new ErrorException($message, 0, $severity, $file, $line); } -} \ No newline at end of file +} diff --git a/Core/DebugBarExceptionHandler.php b/Core/DebugBarExceptionHandler.php index dbfda52..2a962f1 100644 --- a/Core/DebugBarExceptionHandler.php +++ b/Core/DebugBarExceptionHandler.php @@ -31,11 +31,13 @@ class DebugBarExceptionHandler * Handler for uncaught exceptions. * * @param Throwable $exception exception object + * @return void */ - public function handleUncaughtException(Throwable $exception) + public function handleUncaughtException(Throwable $exception): void { try { - $debugMode = (bool) Registry::get( ConfigFile::class)->getVar( 'iDebug'); + /** @var int $debugMode */ + $debugMode = Registry::get(ConfigFile::class)->getVar('iDebug'); $defaultExceptionHandler = new ExceptionHandler($debugMode); $defaultExceptionHandler->writeExceptionToLog($exception); } catch (Throwable $loggerException) { @@ -57,14 +59,14 @@ class DebugBarExceptionHandler if ($debugBarSet !== 1 && false === isAdmin()) { try { /** @var DebugBarComponent $debugBarComponent */ - $debugBarComponent = oxNew( DebugBarComponent::class ); + $debugBarComponent = oxNew(DebugBarComponent::class); /** @var ExceptionsCollector $excCollector */ - $excCollector = $debugBarComponent->getDebugBar()->getCollector( 'exceptions' ); - $excCollector->addThrowable( $exception ); + $excCollector = $debugBarComponent->getDebugBar()->getCollector('exceptions'); + $excCollector->addThrowable($exception); echo << + @@ -82,9 +84,9 @@ HTML; HTML; } catch (DebugBarException $e) { - Registry::getLogger()->error($e); + Registry::getLogger()->error($e->getMessage()); Registry::getUtilsView()->addErrorToDisplay($e); } } } -} \ No newline at end of file +} diff --git a/Modules/Core/Config_DebugBar.php b/Modules/Core/Config_DebugBar.php index e43feeb..fa55d63 100644 --- a/Modules/Core/Config_DebugBar.php +++ b/Modules/Core/Config_DebugBar.php @@ -26,4 +26,4 @@ class Config_DebugBar extends Config_DebugBar_parent { return new DebugBarExceptionHandler(); } -} \ No newline at end of file +} diff --git a/Modules/Core/ShopControl_DebugBar.php b/Modules/Core/ShopControl_DebugBar.php index 9d95d1e..281b7d0 100644 --- a/Modules/Core/ShopControl_DebugBar.php +++ b/Modules/Core/ShopControl_DebugBar.php @@ -38,8 +38,8 @@ class ShopControl_DebugBar extends ShopControl_DebugBar_parent /** * @param string|null $controllerKey * @param string|null $function - * @param string|null $parameters - * @param string|null $viewsChain + * @param array $parameters + * @param array $viewsChain */ public function start($controllerKey = null, $function = null, $parameters = null, $viewsChain = null) { @@ -62,8 +62,9 @@ class ShopControl_DebugBar extends ShopControl_DebugBar_parent /** * @param Throwable $exception + * @return void */ - protected function debugBarHandleException(Throwable $exception) + protected function debugBarHandleException(Throwable $exception): void { $exceptionHandler = new DebugBarExceptionHandler(); $exceptionHandler->handleUncaughtException($exception); diff --git a/Modules/functions.php b/Modules/functions.php index 2c46d1c..18ddf07 100644 --- a/Modules/functions.php +++ b/Modules/functions.php @@ -20,7 +20,8 @@ use DebugBar\DebugBarException; use OxidEsales\Eshop\Core\Registry; /** - * @param $sProfileName + * @param string $sProfileName + * * @return void */ function startProfile(string $sProfileName): void diff --git a/Setup/Events.php b/Setup/Events.php index 3494176..beae88e 100644 --- a/Setup/Events.php +++ b/Setup/Events.php @@ -15,29 +15,19 @@ declare(strict_types=1); namespace D3\DebugBar\Setup; -use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; -use D3\ModCfg\Application\Model\Install\d3install; -use Doctrine\DBAL\DBALException; -use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; -use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; -use OxidEsales\Eshop\Core\Exception\StandardException; -use OxidEsales\Eshop\Core\Exception\SystemComponentException; use OxidEsales\Eshop\Core\Registry; class Events { /** - * @throws d3ShopCompatibilityAdapterException - * @throws DBALException - * @throws DatabaseConnectionException - * @throws DatabaseErrorException - * @throws StandardException - * @throws SystemComponentException + * @return void */ - public static function onActivate() + public static function onActivate(): void { + /** @var string $shopDir */ + $shopDir = Registry::getConfig()->getConfigParam('sShopDir'); if (false === file_exists( - rtrim(Registry::getConfig()->getConfigParam('sShopDir'), '/').'/out/debugbar/debugbar.jas' + rtrim($shopDir, '/').'/out/debugbar/debugbar.jas' )) { Registry::getUtilsView()->addErrorToDisplay( 'The asset files cannot be found. Have you forgotten an installation step described in README? Then please run the installation again.'. @@ -47,7 +37,10 @@ class Events } } - public static function onDeactivate() + /** + * @return void + */ + public static function onDeactivate(): void { } -} \ No newline at end of file +} diff --git a/phpstan.neon b/phpstan.neon index 3699261..00a8587 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,3 +10,8 @@ parameters: ignoreErrors: - '#setConfigParam\(\) expects string, array given.#' - '#Offset .* does not exist on array{function: .*}.#' + - '#ShopControl_DebugBar::_handle.*Exception\(\) has no return type specified#' + - '#ShopControl_DebugBar::start\(\) has no return type specified.#' + - '#Config_DebugBar::getExceptionHandler\(\) should be compatible with return type #' + - '#UtilsView::addErrorToDisplay\(\) expects OxidEsales\\Eshop\\Core\\Contract\\IDisplayError#' + - '#PHPDoc tag @throws with type .*\\ContainerExceptionInterface is not subtype of Throwable#'