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;
/**
* @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;
}

View File

@ -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 = [];

View File

@ -1,4 +1,5 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* 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\Renderable;
use Exception;
use OxidEsales\Eshop\Core\ShopVersion;
use OxidEsales\Eshop\Core\Theme;
use OxidEsales\Facts\Facts;
/**
@ -28,38 +29,35 @@ class OxidVersionCollector extends DataCollector implements Renderable
/**
* @return string
*/
public function getName()
public function getName(): string
{
return 'oxidversion';
}
/**
* @return array
* @throws Exception
*/
public function collect()
public function collect(): array
{
$facts = new Facts();
$theme = new Theme();
$parentThemeId = $theme->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" => "",
],
];
}

View File

@ -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();

View File

@ -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();
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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);
}
}
}

View File

@ -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 <<<HTML
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
@ -82,9 +84,9 @@ HTML;
</html>
HTML;
} catch (DebugBarException $e) {
Registry::getLogger()->error($e);
Registry::getLogger()->error($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
}
}
}

View File

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

View File

@ -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);

View File

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

View File

@ -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 <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:
- '#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#'