add missing type hints

This commit is contained in:
Daniel Seifert 2023-11-23 15:01:44 +01:00
parent 5e573a2435
commit 53a9effeac
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
6 changed files with 110 additions and 57 deletions

View File

@ -14,12 +14,12 @@
declare(strict_types=1); declare(strict_types=1);
use D3\DIContainerHandler\d3DicHandler; use D3\DIContainerHandler\d3DicHandler;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Container;
/** /**
* @return ContainerBuilder * @return Container
*/ */
function d3GetOxidDIC(): ContainerBuilder function d3GetOxidDIC(): Container
{ {
return d3DicHandler::getInstance(); return d3DicHandler::getInstance();
} }

View File

@ -21,6 +21,7 @@ use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Facts\Config\ConfigFile; use OxidEsales\Facts\Config\ConfigFile;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@ -29,30 +30,32 @@ class d3DicHandler implements d3DicHandlerInterface
{ {
/** /**
* instance * instance
* @var array * @var null|Container
*/ */
protected static $_instance; protected static ?Container $_instance = null;
public static $circularReferenceMethodNames = array( public static array $circularReferenceMethodNames = ['getViewConfig'];
'getViewConfig'
);
/** /**
* get instance * get instance
* @return ContainerBuilder *
* @return Container
* @throws Exception
*/ */
public static function getInstance() public static function getInstance(): Container
{ {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1]; $caller = $trace[1];
$functionName = $caller['function']; $functionName = $caller['function'];
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
{ throw oxNew(
throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); Exception::class,
'method '.$functionName." can't use DIC due the danger of circular reference"
);
} }
if (null == self::$_instance) { if (null === self::$_instance) {
$oDicHandler = oxNew(d3DicHandler::class); $oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer(); self::$_instance = $oDicHandler->buildContainer();
} }
@ -62,17 +65,21 @@ class d3DicHandler implements d3DicHandlerInterface
/** /**
* get instance * get instance
* @return ContainerBuilder *
* @return Container
* @throws Exception
*/ */
public static function getUncompiledInstance() public static function getUncompiledInstance(): Container
{ {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1]; $caller = $trace[1];
$functionName = $caller['function']; $functionName = $caller['function'];
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
{ throw oxNew(
throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); Exception::class,
'method '.$functionName." can't use DIC due the danger of circular reference"
);
} }
$oDicHandler = oxNew(d3DicHandler::class); $oDicHandler = oxNew(d3DicHandler::class);
@ -81,7 +88,7 @@ class d3DicHandler implements d3DicHandlerInterface
return self::$_instance; return self::$_instance;
} }
public static function removeInstance() public static function removeInstance(): void
{ {
self::$_instance = null; self::$_instance = null;
} }
@ -89,30 +96,35 @@ class d3DicHandler implements d3DicHandlerInterface
/** /**
* @return Config * @return Config
*/ */
public function d3GetConfig() public function d3GetConfig(): Config
{ {
return Registry::getConfig(); return Registry::getConfig();
} }
public function d3GetCacheFilePath() public function d3GetCacheFilePath(): string
{ {
return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.Registry::getConfig()->getShopId().'.php'; return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.
Registry::getConfig()->getShopId().'.php';
} }
/** /**
* @return d3DIContainerCache|object * @return Container
*/ */
public function d3GetCacheContainer() public function d3GetCacheContainer(): Container
{ {
require_once $this->d3GetCacheFilePath(); require_once $this->d3GetCacheFilePath();
return oxNew(d3DIContainerCache::class);
/** @var Container $container */
$container = oxNew(d3DIContainerCache::class); /** @phpstan-ignore-line */
return $container;
} }
/** /**
* @param $container * @param ContainerBuilder $container
*
* @return YamlFileLoader * @return YamlFileLoader
*/ */
public function d3GetFileLoader($container) public function d3GetFileLoader(ContainerBuilder $container): YamlFileLoader
{ {
/** @var YamlFileLoader $fileLoader */ /** @var YamlFileLoader $fileLoader */
$fileLoader = oxNew(YamlFileLoader::class, $fileLoader = oxNew(YamlFileLoader::class,
@ -124,9 +136,11 @@ class d3DicHandler implements d3DicHandlerInterface
} }
/** /**
* @param $container * @param ContainerBuilder $container
*
* @throws Exception
*/ */
public function loadFiles($container) public function loadFiles(ContainerBuilder $container): void
{ {
$loader = $this->d3GetFileLoader($container); $loader = $this->d3GetFileLoader($container);
@ -141,17 +155,19 @@ class d3DicHandler implements d3DicHandlerInterface
/** /**
* @param bool $compileAndDump * @param bool $compileAndDump
* @return ContainerBuilder *
* @return Container
* @throws Exception
*/ */
public function buildContainer(bool $compileAndDump = true) public function buildContainer(bool $compileAndDump = true): Container
{ {
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) startProfile(__METHOD__); if (Registry::get( ConfigFile::class)->getVar( 'iDebug')) startProfile(__METHOD__);
$config = $this->d3GetConfig(); $config = $this->d3GetConfig();
if ($config->isProductiveMode() if ( $config->isProductiveMode()
&& false == $config->getConfigParam('iDebug') && ! $config->getConfigParam( 'iDebug' )
&& (false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST')) && ( ! defined( 'OXID_PHP_UNIT' ) || defined( 'D3_MODCFG_TEST' ) )
&& file_exists($this->d3GetCacheFilePath()) && file_exists($this->d3GetCacheFilePath())
) { ) {
$container = $this->d3GetCacheContainer(); $container = $this->d3GetCacheContainer();
@ -162,19 +178,19 @@ class d3DicHandler implements d3DicHandlerInterface
if ($compileAndDump) { if ($compileAndDump) {
$container->compile(); $container->compile();
if (false == defined('OXID_PHP_UNIT')) { if ( ! defined( 'OXID_PHP_UNIT' ) ) {
$dumper = new PhpDumper($container); $dumper = new PhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(array('class' => 'd3DIContainerCache'))); file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(array('class' => 'd3DIContainerCache')));
} }
} }
} }
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) stopProfile(__METHOD__); if (Registry::get( ConfigFile::class)->getVar( 'iDebug')) stopProfile(__METHOD__);
return $container; return $container;
} }
public function getContainerBuilder() public function getContainerBuilder(): ContainerBuilder
{ {
return oxNew(ContainerBuilder::class); return oxNew(ContainerBuilder::class);
} }

View File

@ -15,10 +15,12 @@ declare(strict_types=1);
namespace D3\DIContainerHandler; namespace D3\DIContainerHandler;
use Symfony\Component\DependencyInjection\Container;
/** /**
* Interface d3DicHandlerInterface * Interface d3DicHandlerInterface
*/ */
interface d3DicHandlerInterface interface d3DicHandlerInterface
{ {
public static function getInstance(); public static function getInstance(): ?Container;
} }

View File

@ -18,11 +18,12 @@ namespace D3\DIContainerHandler;
class d3DicUtilities class d3DicUtilities
{ {
/** /**
* @param $classNameSpace * @param string $classNameSpace
* @param bool $additional * @param string|null $additional
*
* @return string * @return string
*/ */
public static function getServiceId($classNameSpace, $additional = false) public static function getServiceId(string $classNameSpace, ?string $additional = null): string
{ {
return strtolower( return strtolower(
($additional ? $additional.'.' : ''). ($additional ? $additional.'.' : '').
@ -31,11 +32,12 @@ class d3DicUtilities
} }
/** /**
* @param $classNameSpace * @param string $classNamespace
* @param $argumentName * @param string $argumentName
*
* @return string * @return string
*/ */
public static function getArgumentId($classNamespace, $argumentName) public static function getArgumentId(string $classNamespace, string $argumentName): string
{ {
return strtolower( return strtolower(
$classNamespace. $classNamespace.

View File

@ -15,15 +15,17 @@ declare(strict_types=1);
namespace D3\DIContainerHandler; namespace D3\DIContainerHandler;
use InvalidArgumentException;
class definitionFileContainer class definitionFileContainer
{ {
public const TYPE_YAML = 'yml'; public const TYPE_YAML = 'yml';
protected $definitionFiles = [ protected array $definitionFiles = [
self::TYPE_YAML => [] self::TYPE_YAML => []
]; ];
protected $allowedTypes = [ protected array $allowedTypes = [
self::TYPE_YAML self::TYPE_YAML
]; ];
@ -32,30 +34,49 @@ class definitionFileContainer
$this->addYamlDefinitions('d3/modcfg/Config/services.yaml'); $this->addYamlDefinitions('d3/modcfg/Config/services.yaml');
} }
public function addDefinitions($definitionFile, $type) /**
* @param string $definitionFile
* @param string $type
*
* @return void
*/
public function addDefinitions(string $definitionFile, string $type): void
{ {
if (!in_array($type, $this->allowedTypes)) { if (!in_array($type, $this->allowedTypes)) {
throw new \InvalidArgumentException('invalid definition file type'); throw new InvalidArgumentException( 'invalid definition file type');
} }
$this->definitionFiles[$type][md5($definitionFile)] = $definitionFile; $this->definitionFiles[$type][md5($definitionFile)] = $definitionFile;
} }
public function addYamlDefinitions($definitionFile) /**
* @param string $definitionFile
*
* @return void
*/
public function addYamlDefinitions(string $definitionFile): void
{ {
$this->addDefinitions($definitionFile, self::TYPE_YAML); $this->addDefinitions($definitionFile, self::TYPE_YAML);
} }
public function getDefinitions($type) /**
* @param string $type
*
* @return array
*/
public function getDefinitions(string $type): array
{ {
if (!in_array($type, $this->allowedTypes)) { if (!in_array($type, $this->allowedTypes)) {
throw new \InvalidArgumentException('invalid definition file type'); throw new InvalidArgumentException( 'invalid definition file type');
} }
return $this->definitionFiles[$type]; return $this->definitionFiles[$type];
} }
public function getYamlDefinitions() /**
* @return array
*/
public function getYamlDefinitions(): array
{ {
return $this->getDefinitions(self::TYPE_YAML); return $this->getDefinitions(self::TYPE_YAML);
} }
@ -64,17 +85,17 @@ class definitionFileContainer
* @param string $definitionFile * @param string $definitionFile
* @return bool * @return bool
*/ */
public function has($definitionFile): bool public function has(string $definitionFile): bool
{ {
return isset($this->definitionFiles[md5($definitionFile)]); return isset($this->definitionFiles[md5($definitionFile)]);
} }
public function getAll() public function getAll(): array
{ {
return $this->definitionFiles; return $this->definitionFiles;
} }
public function clear() public function clear(): void
{ {
$this->definitionFiles = []; $this->definitionFiles = [];
} }

12
phpstan.neon Normal file
View File

@ -0,0 +1,12 @@
parameters:
scanFiles:
- ../../oxid-esales/oxideshop-ce/source/bootstrap.php
- ../../oxid-esales/oxideshop-ce/source/oxfunctions.php
- ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php
paths:
- .
level: 0
phpVersion: 80300
checkMissingIterableValueType: false
featureToggles:
disableRuntimeReflectionProvider: true