cleanup code

This commit is contained in:
Daniel Seifert 2022-08-16 11:08:53 +02:00
parent 922d26d3ac
commit 6eb4c213b9
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
16 changed files with 118 additions and 91 deletions

View File

@ -36,6 +36,11 @@ class OxidConfigCollector extends DataCollector implements Renderable
*/ */
protected $useHtmlVarDumper = false; protected $useHtmlVarDumper = false;
/**
* @param Config $config
*
* @throws ReflectionException
*/
public function __construct(Config $config) public function __construct(Config $config)
{ {
$config->init(); $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 * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering. * rendering.
* *
* @return mixed * @return bool
*/ */
public function isHtmlVarDumperUsed() public function isHtmlVarDumperUsed(): bool
{ {
return $this->useHtmlVarDumper; return $this->useHtmlVarDumper;
} }

View File

@ -18,13 +18,17 @@ namespace D3\DebugBar\Application\Models\Collectors;
use Composer\InstalledVersions; use Composer\InstalledVersions;
use DebugBar\DataCollector\DataCollector; use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable; use DebugBar\DataCollector\Renderable;
use Exception;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Module\Module; use OxidEsales\Eshop\Core\Module\Module;
use OxidEsales\Eshop\Core\ShopVersion; use OxidEsales\Eshop\Core\ShopVersion;
use OxidEsales\Eshop\Core\Theme; use OxidEsales\Eshop\Core\Theme;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; 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\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface;
use OxidEsales\Facts\Facts; use OxidEsales\Facts\Facts;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class OxidShopCollector extends DataCollector implements Renderable class OxidShopCollector extends DataCollector implements Renderable
{ {
@ -39,11 +43,18 @@ class OxidShopCollector extends DataCollector implements Renderable
*/ */
protected $useHtmlVarDumper = true; protected $useHtmlVarDumper = true;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function __construct() public function __construct()
{ {
$facts = new Facts(); $facts = new Facts();
$theme = new Theme(); $theme = new Theme();
$parentThemeId = $theme->getParent() ? $theme->getParent()->getId() : '--'; /** @var Theme|null $parent */
$parent = $theme->getParent();
$parentThemeId = $parent ? $parent->getId() : '--';
$moduleList = $this->getInstalledModules(); $moduleList = $this->getInstalledModules();
array_walk( array_walk(
@ -60,7 +71,7 @@ class OxidShopCollector extends DataCollector implements Renderable
'CE Version:' => InstalledVersions::getVersion('oxid-esales/oxideshop-ce'), 'CE Version:' => InstalledVersions::getVersion('oxid-esales/oxideshop-ce'),
'Theme:' => $theme->getActiveThemeId(), 'Theme:' => $theme->getActiveThemeId(),
'Parent Theme:' => $parentThemeId, '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 * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering. * rendering.
* *
* @return mixed * @return bool
*/ */
public function isHtmlVarDumperUsed() public function isHtmlVarDumperUsed(): bool
{ {
return $this->useHtmlVarDumper; return $this->useHtmlVarDumper;
} }
@ -121,10 +132,17 @@ class OxidShopCollector extends DataCollector implements Renderable
]; ];
} }
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function getInstalledModules(): array protected function getInstalledModules(): array
{ {
$container = ContainerFactory::getInstance()->getContainer(); $container = ContainerFactory::getInstance()->getContainer();
$shopConfiguration = $container->get(ShopConfigurationDaoBridgeInterface::class)->get(); /** @var ShopConfigurationDaoBridge $shopConfigurationDaoBridge */
$shopConfigurationDaoBridge = $container->get(ShopConfigurationDaoBridgeInterface::class);
$shopConfiguration = $shopConfigurationDaoBridge->get();
$modules = []; $modules = [];

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@ -16,8 +17,8 @@ namespace D3\DebugBar\Application\Models\Collectors;
use DebugBar\DataCollector\DataCollector; use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable; use DebugBar\DataCollector\Renderable;
use Exception;
use OxidEsales\Eshop\Core\ShopVersion; use OxidEsales\Eshop\Core\ShopVersion;
use OxidEsales\Eshop\Core\Theme;
use OxidEsales\Facts\Facts; use OxidEsales\Facts\Facts;
/** /**
@ -28,38 +29,35 @@ class OxidVersionCollector extends DataCollector implements Renderable
/** /**
* @return string * @return string
*/ */
public function getName() public function getName(): string
{ {
return 'oxidversion'; return 'oxidversion';
} }
/** /**
* @return array * @return array
* @throws Exception
*/ */
public function collect() public function collect(): array
{ {
$facts = new Facts(); $facts = new Facts();
$theme = new Theme();
$parentThemeId = $theme->getParent() ? $theme->getParent()->getId() : '--';
return array( return [
'version' => $facts->getEdition().' '.ShopVersion::getVersion() 'version' => $facts->getEdition().' '.ShopVersion::getVersion(),
); ];
} }
/** /**
* {@inheritDoc} * @return string[][]
*/ */
public function getWidgets() public function getWidgets(): array
{ {
$collect = $this->collect();
return [ return [
"oxidversion" => [ "oxidversion" => [
"icon" => "shopping-cart", "icon" => "shopping-cart",
"tooltip" => 'OXID Version', "tooltip" => 'OXID Version',
"map" => $this->getName().".version", "map" => $this->getName().".version",
"default" => "" "default" => "",
], ],
]; ];
} }

View File

@ -38,7 +38,7 @@ class SmartyCollector extends DataCollector implements Renderable
* @param bool $value * @param bool $value
* @return $this * @return $this
*/ */
public function useHtmlVarDumper($value = true) public function useHtmlVarDumper(bool $value = true): SmartyCollector
{ {
$this->useHtmlVarDumper = $value; $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 * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering. * rendering.
* *
* @return mixed * @return bool
*/ */
public function isHtmlVarDumperUsed() public function isHtmlVarDumperUsed(): bool
{ {
return $this->useHtmlVarDumper; return $this->useHtmlVarDumper;
} }
@ -69,7 +69,7 @@ class SmartyCollector extends DataCollector implements Renderable
*/ */
public function collect(): array public function collect(): array
{ {
$data = []; $data = ['current view template' => Registry::getConfig()->getTopActiveView()->getTemplateName()];
$vars = $this->smarty->get_template_vars(); $vars = $this->smarty->get_template_vars();

View File

@ -28,13 +28,12 @@ class DebugBarHandler
public function setErrorHandler(): void public function setErrorHandler(): void
{ {
if ($this->d3CanActivateDebugBar()) { if ($this->d3CanActivateDebugBar()) {
set_error_handler( /** @var callable $callable */
[ $callable = [
new DebugBarErrorHandler(), new DebugBarErrorHandler(),
'callback' 'callback',
], ];
$this->getHandledErrorTypes() set_error_handler($callable, $this->getHandledErrorTypes());
);
} }
} }
@ -52,10 +51,10 @@ class DebugBarHandler
public function setExceptionHandler(): void public function setExceptionHandler(): void
{ {
if ($this->d3CanActivateDebugBar()) { if ($this->d3CanActivateDebugBar()) {
set_exception_handler( [ set_exception_handler([
new DebugBarExceptionHandler(), new DebugBarExceptionHandler(),
'handleUncaughtException' 'handleUncaughtException',
] ); ]);
} }
} }
@ -65,17 +64,17 @@ class DebugBarHandler
public function addDebugBarComponent(): void public function addDebugBarComponent(): void
{ {
if ($this->d3CanActivateDebugBar()) { if ($this->d3CanActivateDebugBar()) {
$userComponentNames = Registry::getConfig()->getConfigParam( 'aUserComponentNames' ); $userComponentNames = Registry::getConfig()->getConfigParam('aUserComponentNames');
$d3CmpName = DebugBarComponent::class; $d3CmpName = DebugBarComponent::class;
$blDontUseCache = 1; $blDontUseCache = 1;
if ( ! is_array( $userComponentNames ) ) { if (! is_array($userComponentNames)) {
$userComponentNames = []; $userComponentNames = [];
} }
if ( ! in_array( $d3CmpName, array_keys( $userComponentNames ) ) ) { if (! in_array($d3CmpName, array_keys($userComponentNames))) {
$userComponentNames[ $d3CmpName ] = $blDontUseCache; $userComponentNames[ $d3CmpName ] = $blDontUseCache;
Registry::getConfig()->setConfigParam( 'aUserComponentNames', $userComponentNames ); Registry::getConfig()->setConfigParam('aUserComponentNames', $userComponentNames);
} }
} }
} }
@ -87,4 +86,4 @@ class DebugBarHandler
{ {
return false === isAdmin(); return false === isAdmin();
} }
} }

View File

@ -19,4 +19,4 @@ use ErrorException;
class CompileErrorException extends ErrorException class CompileErrorException extends ErrorException
{ {
} }

View File

@ -19,4 +19,4 @@ use ErrorException;
class CoreErrorException extends ErrorException class CoreErrorException extends ErrorException
{ {
} }

View File

@ -19,4 +19,4 @@ use ErrorException;
class ParseException extends ErrorException class ParseException extends ErrorException
{ {
} }

View File

@ -19,4 +19,4 @@ use ErrorException;
class UserErrorException extends ErrorException class UserErrorException extends ErrorException
{ {
} }

View File

@ -25,10 +25,10 @@ use OxidEsales\Eshop\Core\Registry;
class DebugBarErrorHandler class DebugBarErrorHandler
{ {
/** /**
* @param $severity * @param int $severity
* @param $message * @param string $message
* @param $file * @param string $file
* @param $line * @param int $line
* *
* @return void|false * @return void|false
* @throws CompileErrorException * @throws CompileErrorException
@ -37,22 +37,22 @@ class DebugBarErrorHandler
* @throws ParseException * @throws ParseException
* @throws UserErrorException * @throws UserErrorException
*/ */
public function callback( $severity, $message, $file, $line ) public function callback(int $severity, string $message, string $file, int $line)
{ {
global $debugBarErrorOccured; global $debugBarErrorOccured;
$debugBarErrorOccured = 1; $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. // This error code is not included in error_reporting.
return false; return false;
} }
$smartyTemplate = $this->getSmartyTemplateLocationFromError( $message ); $smartyTemplate = $this->getSmartyTemplateLocationFromError($message);
if ( is_array( $smartyTemplate ) ) { if (is_array($smartyTemplate)) {
[ $file, $line ] = $smartyTemplate; [ $file, $line ] = $smartyTemplate;
} }
switch($severity) { switch ($severity) {
case E_CORE_ERROR: case E_CORE_ERROR:
throw new CoreErrorException($message, 0, $severity, $file, $line); throw new CoreErrorException($message, 0, $severity, $file, $line);
case E_COMPILE_ERROR: case E_COMPILE_ERROR:
@ -69,10 +69,10 @@ class DebugBarErrorHandler
} }
/** /**
* @param $messsage * @param string $messsage
* @return array|null * @return array|null
*/ */
protected function getSmartyTemplateLocationFromError($messsage): ?array protected function getSmartyTemplateLocationFromError(string $messsage): ?array
{ {
if (stristr($messsage, 'Smarty error: [in ')) { if (stristr($messsage, 'Smarty error: [in ')) {
$start = strpos($messsage, '[')+1; $start = strpos($messsage, '[')+1;
@ -85,15 +85,20 @@ class DebugBarErrorHandler
} }
/** /**
* @param string $message * @param string $message
* @param int|null $severity * @param int $severity
* @param string|null $file * @param string $file
* @param int|null $line * @param int $line
* @return void
* *
* @throws ErrorException * @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); throw new ErrorException($message, 0, $severity, $file, $line);
} }
} }

View File

@ -31,11 +31,13 @@ class DebugBarExceptionHandler
* Handler for uncaught exceptions. * Handler for uncaught exceptions.
* *
* @param Throwable $exception exception object * @param Throwable $exception exception object
* @return void
*/ */
public function handleUncaughtException(Throwable $exception) public function handleUncaughtException(Throwable $exception): void
{ {
try { try {
$debugMode = (bool) Registry::get( ConfigFile::class)->getVar( 'iDebug'); /** @var int $debugMode */
$debugMode = Registry::get(ConfigFile::class)->getVar('iDebug');
$defaultExceptionHandler = new ExceptionHandler($debugMode); $defaultExceptionHandler = new ExceptionHandler($debugMode);
$defaultExceptionHandler->writeExceptionToLog($exception); $defaultExceptionHandler->writeExceptionToLog($exception);
} catch (Throwable $loggerException) { } catch (Throwable $loggerException) {
@ -57,14 +59,14 @@ class DebugBarExceptionHandler
if ($debugBarSet !== 1 && false === isAdmin()) { if ($debugBarSet !== 1 && false === isAdmin()) {
try { try {
/** @var DebugBarComponent $debugBarComponent */ /** @var DebugBarComponent $debugBarComponent */
$debugBarComponent = oxNew( DebugBarComponent::class ); $debugBarComponent = oxNew(DebugBarComponent::class);
/** @var ExceptionsCollector $excCollector */ /** @var ExceptionsCollector $excCollector */
$excCollector = $debugBarComponent->getDebugBar()->getCollector( 'exceptions' ); $excCollector = $debugBarComponent->getDebugBar()->getCollector('exceptions');
$excCollector->addThrowable( $exception ); $excCollector->addThrowable($exception);
echo <<<HTML echo <<<HTML
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title></title> <title></title>
@ -82,9 +84,9 @@ HTML;
</html> </html>
HTML; HTML;
} catch (DebugBarException $e) { } catch (DebugBarException $e) {
Registry::getLogger()->error($e); Registry::getLogger()->error($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e); Registry::getUtilsView()->addErrorToDisplay($e);
} }
} }
} }
} }

View File

@ -26,4 +26,4 @@ class Config_DebugBar extends Config_DebugBar_parent
{ {
return new DebugBarExceptionHandler(); return new DebugBarExceptionHandler();
} }
} }

View File

@ -38,8 +38,8 @@ class ShopControl_DebugBar extends ShopControl_DebugBar_parent
/** /**
* @param string|null $controllerKey * @param string|null $controllerKey
* @param string|null $function * @param string|null $function
* @param string|null $parameters * @param array $parameters
* @param string|null $viewsChain * @param array $viewsChain
*/ */
public function start($controllerKey = null, $function = null, $parameters = null, $viewsChain = null) 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 * @param Throwable $exception
* @return void
*/ */
protected function debugBarHandleException(Throwable $exception) protected function debugBarHandleException(Throwable $exception): void
{ {
$exceptionHandler = new DebugBarExceptionHandler(); $exceptionHandler = new DebugBarExceptionHandler();
$exceptionHandler->handleUncaughtException($exception); $exceptionHandler->handleUncaughtException($exception);

View File

@ -20,7 +20,8 @@ use DebugBar\DebugBarException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
/** /**
* @param $sProfileName * @param string $sProfileName
*
* @return void * @return void
*/ */
function startProfile(string $sProfileName): void function startProfile(string $sProfileName): void

View File

@ -15,29 +15,19 @@ declare(strict_types=1);
namespace D3\DebugBar\Setup; 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; use OxidEsales\Eshop\Core\Registry;
class Events class Events
{ {
/** /**
* @throws d3ShopCompatibilityAdapterException * @return void
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws StandardException
* @throws SystemComponentException
*/ */
public static function onActivate() public static function onActivate(): void
{ {
/** @var string $shopDir */
$shopDir = Registry::getConfig()->getConfigParam('sShopDir');
if (false === file_exists( if (false === file_exists(
rtrim(Registry::getConfig()->getConfigParam('sShopDir'), '/').'/out/debugbar/debugbar.jas' rtrim($shopDir, '/').'/out/debugbar/debugbar.jas'
)) { )) {
Registry::getUtilsView()->addErrorToDisplay( Registry::getUtilsView()->addErrorToDisplay(
'The asset files cannot be found. Have you forgotten an installation step described in <a href="https://git.d3data.de/D3Public/DebugBar/src/branch/main/README.en.md">README</a>? Then please run the installation again.'. 'The asset files cannot be found. Have you forgotten an installation step described in <a href="https://git.d3data.de/D3Public/DebugBar/src/branch/main/README.en.md">README</a>? Then please run the installation again.'.
@ -47,7 +37,10 @@ class Events
} }
} }
public static function onDeactivate() /**
* @return void
*/
public static function onDeactivate(): void
{ {
} }
} }

View File

@ -10,3 +10,8 @@ parameters:
ignoreErrors: ignoreErrors:
- '#setConfigParam\(\) expects string, array given.#' - '#setConfigParam\(\) expects string, array given.#'
- '#Offset .* does not exist on array{function: .*}.#' - '#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#'