improve code for PHP 8

This commit is contained in:
Daniel Seifert 2024-03-04 13:57:10 +01:00
parent 95e1002df5
commit c207a03287
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
9 changed files with 126 additions and 124 deletions

View File

@ -18,20 +18,21 @@ namespace D3\DataWizard\Application\Controller\Admin;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\ModCfg\Application\Model\d3database;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception as DBALException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class d3ActionWizard extends AdminDetailsController
{
protected $_sThisTemplate = 'd3ActionWizard.tpl';
/** @var Configuration */
protected $configuration;
protected Configuration $configuration;
public function __construct()
{
@ -51,23 +52,28 @@ class d3ActionWizard extends AdminDetailsController
}
/**
* @throws DatabaseConnectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function runTask()
public function runTask(): void
{
try {
$this->execute();
} catch (DataWizardException|DBALException|DatabaseErrorException $e) {
} catch (DataWizardException|DBALException $e) {
Registry::getLogger()->error($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
/**
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws DebugException
* @throws InputUnvalidException
* @throws NotFoundExceptionInterface
* @throws TaskException
*/
protected function execute()
protected function execute(): void
{
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$action = $this->configuration->getActionById($id);
@ -75,10 +81,12 @@ class d3ActionWizard extends AdminDetailsController
[ $queryString, $parameters ] = $action->getQuery();
if ($this->d3GetConfig()->getConfigParam('d3datawizard_debug')) {
throw oxNew(
/** @var DebugException $debug */
$debug = oxNew(
DebugException::class,
d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters)
);
throw $debug;
}
$action->run();
@ -92,12 +100,12 @@ class d3ActionWizard extends AdminDetailsController
return Registry::getConfig();
}
public function getUserMessages()
public function getUserMessages(): ?string
{
return null;
}
public function getHelpURL()
public function getHelpURL(): ?string
{
return null;
}

View File

@ -19,24 +19,25 @@ use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\ModCfg\Application\Model\d3database;
use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception as DBALException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class d3ExportWizard extends AdminDetailsController
{
protected $_sThisTemplate = 'd3ExportWizard.tpl';
/** @var Configuration */
protected $configuration;
protected Configuration $configuration;
public function __construct()
{
@ -56,12 +57,15 @@ class d3ExportWizard extends AdminDetailsController
}
/**
* @throws ContainerExceptionInterface
* @throws DatabaseConnectionException
* @throws Exception
* @throws NotFoundExceptionInterface
* @throws StandardException
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
*/
public function runTask()
public function runTask(): void
{
try {
$this->execute();
@ -75,13 +79,15 @@ class d3ExportWizard extends AdminDetailsController
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws StandardException
* @throws NoSuitableRendererException
* @throws TaskException
* @throws StandardException
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
*/
protected function execute()
protected function execute(): void
{
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$export = $this->configuration->getExportById($id);
@ -106,12 +112,12 @@ class d3ExportWizard extends AdminDetailsController
return Registry::getConfig();
}
public function getUserMessages()
public function getUserMessages(): ?string
{
return null;
}
public function getHelpURL()
public function getHelpURL(): ?string
{
return null;
}

View File

@ -16,18 +16,20 @@ declare(strict_types=1);
namespace D3\DataWizard\Application\Model;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use Doctrine\DBAL\Exception as DBALException;
use FormManager\Inputs\Checkbox;
use FormManager\Inputs\Input;
use FormManager\Inputs\Radio;
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
abstract class ActionBase implements QueryBase
{
protected $formElements = [];
protected array $formElements = [];
/**
* Ensure that the translations are equally available in the frontend and the backend
@ -39,16 +41,21 @@ abstract class ActionBase implements QueryBase
}
/**
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws InputUnvalidException
* @throws NotFoundExceptionInterface
* @throws TaskException
*/
public function run()
public function run(): void
{
if ($this->hasFormElements()) {
/** @var Input $element */
foreach ($this->getFormElements() as $element) {
if (false === $element->isValid()) {
throw oxNew(InputUnvalidException::class, $this, $element);
/** @var InputUnvalidException $exception */
$exception = oxNew(InputUnvalidException::class, $this, $element);
throw $exception;
}
}
}
@ -59,28 +66,34 @@ abstract class ActionBase implements QueryBase
/**
* @param array $query
*
* @return int
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws TaskException
* @throws DBALException
*/
public function executeAction(array $query): int
public function executeAction(array $query): void
{
[ $queryString, $parameters ] = $query;
$queryString = trim($queryString);
if (strtolower(substr($queryString, 0, 6)) === 'select') {
throw oxNew(
Exceptions\TaskException::class,
/** @var TaskException $exception */
$exception = oxNew(
TaskException::class,
$this,
Registry::getLang()->translateString('D3_DATAWIZARD_ERR_ACTIONSELECT')
);
throw $exception;
}
$affected = $this->d3GetDb()->execute($queryString, $parameters);
$connection = ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
$affected = (int) $connection->executeStatement($queryString, $parameters);
throw oxNew(
Exceptions\TaskException::class,
/** @var TaskException $exception */
$exception = oxNew(
TaskException::class,
$this,
sprintf(
Registry::getLang()->translateString(
@ -89,15 +102,7 @@ abstract class ActionBase implements QueryBase
$affected
)
);
}
/**
* @return DatabaseInterface|null
* @throws DatabaseConnectionException
*/
public function d3GetDb(): ?DatabaseInterface
{
return DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
throw $exception;
}
/**
@ -111,7 +116,7 @@ abstract class ActionBase implements QueryBase
/**
* @param Input $input
*/
public function registerFormElement(Input $input)
public function registerFormElement(Input $input): void
{
if ($input instanceof Radio || $input instanceof Checkbox) {
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');

View File

@ -27,8 +27,8 @@ class Configuration
public const GROUP_REMARKS = 'D3_DATAWIZARD_GROUP_REMARKS';
public const GROUP_CMS = 'D3_DATAWIZARD_GROUP_CMS';
protected $actions = [];
protected $exports = [];
protected array $actions = [];
protected array $exports = [];
public function __construct()
{
@ -44,7 +44,7 @@ class Configuration
* @param $group
* @param ActionBase $action
*/
public function registerAction($group, ActionBase $action)
public function registerAction($group, ActionBase $action): void
{
$this->actions[$group][md5(get_class($action))] = $action;
}
@ -53,7 +53,7 @@ class Configuration
* @param $group
* @param ExportBase $export
*/
public function registerExport($group, ExportBase $export)
public function registerExport($group, ExportBase $export): void
{
$this->exports[$group][md5(get_class($export))] = $export;
}
@ -147,7 +147,7 @@ class Configuration
{
$allActions = $this->getAllActions();
if (false == $allActions[$id]) {
if ( ! $allActions[ $id ] ) {
throw oxNew(DataWizardException::class, 'no action with id '.$id);
}
@ -163,7 +163,7 @@ class Configuration
{
$allExports = $this->getAllExports();
if (false == $allExports[$id]) {
if ( ! $allExports[ $id ] ) {
throw oxNew(DataWizardException::class, 'no export with id '.$id);
}

View File

@ -21,8 +21,7 @@ use FormManager\Inputs\Input;
class InputUnvalidException extends DataWizardException
{
/** @var QueryBase */
public $task;
public QueryBase $task;
/**
* InputUnvalidException constructor.

View File

@ -20,8 +20,7 @@ use Exception;
class TaskException extends DataWizardException
{
/** @var QueryBase */
public $task;
public QueryBase $task;
public function __construct(QueryBase $task, $sMessage = "not set", $iCode = 0, Exception $previous = null)
{

View File

@ -17,24 +17,30 @@ namespace D3\DataWizard\Application\Model;
use D3\DataWizard\Application\Model\Exceptions\ExportFileException;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\Application\Model\ExportRenderer\RendererInterface;
use D3\ModCfg\Application\Model\d3filesystem;
use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception as DBALException;
use FormManager\Inputs\Checkbox;
use FormManager\Inputs\Input;
use FormManager\Inputs\Radio;
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
abstract class ExportBase implements QueryBase
{
protected $formElements = [];
protected array $formElements = [];
/**
* Ensure that the translations are equally available in the frontend and the backend
@ -47,17 +53,19 @@ abstract class ExportBase implements QueryBase
/**
* @param string $format
* @param $path
* @param null $path
*
* @return string
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exceptions\NoSuitableRendererException
* @throws Exceptions\TaskException
* @throws Exception
* @throws NoSuitableRendererException
* @throws NotFoundExceptionInterface
* @throws StandardException
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
* @return string
*/
public function run(string $format = RendererBridge::FORMAT_CSV, $path = null): string
{
@ -82,31 +90,22 @@ abstract class ExportBase implements QueryBase
}
/**
* @param $format
*
* @return ExportRenderer\RendererInterface
* @throws Exceptions\NoSuitableRendererException
* @throws NoSuitableRendererException
*/
public function getRenderer($format): ExportRenderer\RendererInterface
public function getRenderer(string $format): RendererInterface
{
return $this->getRendererBridge()->getRenderer($format);
}
/**
* @return RendererBridge
*/
public function getRendererBridge(): RendererBridge
{
return oxNew(RendererBridge::class);
}
/**
* @param $format
*
* @return string
* @throws Exceptions\NoSuitableRendererException
* @throws NoSuitableRendererException
*/
public function getFileExtension($format): string
public function getFileExtension(string $format): string
{
return $this->getRenderer($format)->getFileExtension();
}
@ -148,8 +147,10 @@ abstract class ExportBase implements QueryBase
* @param array $query
*
* @return array
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws DBALException
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getExportData(array $query): array
{
@ -165,7 +166,9 @@ abstract class ExportBase implements QueryBase
);
}
$rows = $this->d3GetDb()->getAll($queryString, $parameters);
/** @var Connection $connection */
$connection = ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
$rows = $connection->executeQuery($queryString, $parameters)->fetchAllAssociative();
if (count($rows) <= 0) {
throw oxNew(
@ -180,10 +183,7 @@ abstract class ExportBase implements QueryBase
return [ $rows, $fieldNames ];
}
/**
* @param Input $input
*/
public function registerFormElement(Input $input)
public function registerFormElement(Input $input): void
{
if ($input instanceof Radio || $input instanceof Checkbox) {
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
@ -213,12 +213,16 @@ abstract class ExportBase implements QueryBase
/**
* @param string $format
* @param $path
* @param $path
*
* @return string
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exceptions\NoSuitableRendererException
* @throws Exception
* @throws NoSuitableRendererException
* @throws NotFoundExceptionInterface
* @throws StandardException
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
@ -227,13 +231,12 @@ abstract class ExportBase implements QueryBase
{
$content = $this->getContent($format);
/** @var $oFS d3filesystem */
$oFS = $this->getFileSystem();
if (is_null($path)) {
$oFS->startDirectDownload($oFS->filterFilename($this->getExportFileName($format)), $content);
} else {
$filePath = $oFS->trailingslashit($path) . $oFS->filterFilename($this->getExportFileName($format));
if (false === $oFS->createFile($filePath, $content, true)) {
if (false === $oFS->createFile($filePath, $content)) {
throw oxNew(ExportFileException::class, $filePath);
}
return $filePath;
@ -242,19 +245,7 @@ abstract class ExportBase implements QueryBase
return '';
}
/**
* @return DatabaseInterface|null
* @throws DatabaseConnectionException
*/
protected function d3GetDb(): ?DatabaseInterface
{
return DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
}
/**
* @return d3filesystem|mixed
*/
protected function getFileSystem()
protected function getFileSystem(): d3filesystem
{
return oxNew(d3filesystem::class);
}
@ -263,16 +254,16 @@ abstract class ExportBase implements QueryBase
* @param string $format
*
* @return string
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exceptions\NoSuitableRendererException
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws Exception
* @throws NoSuitableRendererException
* @throws NotFoundExceptionInterface
*/
public function getContent(string $format): string
{
[ $rows, $fieldNames ] = $this->getExportData($this->getQuery());
$content = $this->renderContent($rows, $fieldNames, $format);
return $content;
return $this->renderContent($rows, $fieldNames, $format);
}
}

View File

@ -60,16 +60,10 @@ class Csv implements RendererInterface
EncloseField::addTo($csv, "\t\x1f");
$sEncloser = $this->d3GetConfig()->getConfigParam('sGiCsvFieldEncloser');
if (false == $sEncloser) {
$sEncloser = '"';
}
$sEncloser = $this->d3GetConfig()->getConfigParam('sGiCsvFieldEncloser') ?? '"';
$csv->setEnclosure($sEncloser);
$sDelimiter = $this->d3GetConfig()->getConfigParam('sCSVSign');
if (false == $sDelimiter) {
$sDelimiter = ';';
}
$sDelimiter = $this->d3GetConfig()->getConfigParam('sCSVSign') ?? ';';
$csv->setDelimiter($sDelimiter);
return $csv;

View File

@ -45,7 +45,7 @@ class RendererBridge
/**
* @param RendererInterface $instance
*/
protected function translateRendererId(RendererInterface &$instance)
protected function translateRendererId(RendererInterface &$instance): void
{
$instance = $instance->getTitleTranslationId();
}
@ -60,8 +60,8 @@ class RendererBridge
{
$format = strtolower($format);
$rendererList = array_change_key_case($this->getRendererList(), CASE_LOWER);
$rendererListTypes = array_keys(array_change_key_case($rendererList, CASE_LOWER));
$rendererList = array_change_key_case($this->getRendererList());
$rendererListTypes = array_keys(array_change_key_case($rendererList));
if (in_array($format, $rendererListTypes, true)) {
return $rendererList[$format];