From 53a9effeac6f9f7b45289e696997ec8465ac95a8 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Thu, 23 Nov 2023 15:01:44 +0100 Subject: [PATCH] add missing type hints --- autoload/functions_oxDIC.php | 6 +-- d3DicHandler.php | 88 +++++++++++++++++++++--------------- d3DicHandlerInterface.php | 4 +- d3DicUtilities.php | 14 +++--- definitionFileContainer.php | 43 +++++++++++++----- phpstan.neon | 12 +++++ 6 files changed, 110 insertions(+), 57 deletions(-) create mode 100644 phpstan.neon diff --git a/autoload/functions_oxDIC.php b/autoload/functions_oxDIC.php index dce0f68..13c5d86 100644 --- a/autoload/functions_oxDIC.php +++ b/autoload/functions_oxDIC.php @@ -14,12 +14,12 @@ declare(strict_types=1); 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(); } diff --git a/d3DicHandler.php b/d3DicHandler.php index d800ba3..b9d8921 100644 --- a/d3DicHandler.php +++ b/d3DicHandler.php @@ -21,6 +21,7 @@ use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Facts\Config\ConfigFile; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; @@ -29,30 +30,32 @@ class d3DicHandler implements d3DicHandlerInterface { /** * instance - * @var array + * @var null|Container */ - protected static $_instance; + protected static ?Container $_instance = null; - public static $circularReferenceMethodNames = array( - 'getViewConfig' - ); + public static array $circularReferenceMethodNames = ['getViewConfig']; /** * get instance - * @return ContainerBuilder + * + * @return Container + * @throws Exception */ - public static function getInstance() + public static function getInstance(): Container { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $caller = $trace[1]; $functionName = $caller['function']; - if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) - { - throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); + if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { + throw oxNew( + 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); self::$_instance = $oDicHandler->buildContainer(); } @@ -62,17 +65,21 @@ class d3DicHandler implements d3DicHandlerInterface /** * get instance - * @return ContainerBuilder + * + * @return Container + * @throws Exception */ - public static function getUncompiledInstance() + public static function getUncompiledInstance(): Container { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $caller = $trace[1]; $functionName = $caller['function']; - if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) - { - throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); + if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { + throw oxNew( + Exception::class, + 'method '.$functionName." can't use DIC due the danger of circular reference" + ); } $oDicHandler = oxNew(d3DicHandler::class); @@ -81,7 +88,7 @@ class d3DicHandler implements d3DicHandlerInterface return self::$_instance; } - public static function removeInstance() + public static function removeInstance(): void { self::$_instance = null; } @@ -89,30 +96,35 @@ class d3DicHandler implements d3DicHandlerInterface /** * @return Config */ - public function d3GetConfig() + public function d3GetConfig(): Config { 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(); - return oxNew(d3DIContainerCache::class); + + /** @var Container $container */ + $container = oxNew(d3DIContainerCache::class); /** @phpstan-ignore-line */ + return $container; } /** - * @param $container + * @param ContainerBuilder $container + * * @return YamlFileLoader */ - public function d3GetFileLoader($container) + public function d3GetFileLoader(ContainerBuilder $container): YamlFileLoader { /** @var YamlFileLoader $fileLoader */ $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); @@ -141,17 +155,19 @@ class d3DicHandler implements d3DicHandlerInterface /** * @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(); - if ($config->isProductiveMode() - && false == $config->getConfigParam('iDebug') - && (false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST')) + if ( $config->isProductiveMode() + && ! $config->getConfigParam( 'iDebug' ) + && ( ! defined( 'OXID_PHP_UNIT' ) || defined( 'D3_MODCFG_TEST' ) ) && file_exists($this->d3GetCacheFilePath()) ) { $container = $this->d3GetCacheContainer(); @@ -162,19 +178,19 @@ class d3DicHandler implements d3DicHandlerInterface if ($compileAndDump) { $container->compile(); - if (false == defined('OXID_PHP_UNIT')) { + if ( ! defined( 'OXID_PHP_UNIT' ) ) { $dumper = new PhpDumper($container); 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; } - public function getContainerBuilder() + public function getContainerBuilder(): ContainerBuilder { return oxNew(ContainerBuilder::class); } diff --git a/d3DicHandlerInterface.php b/d3DicHandlerInterface.php index 1d1d156..f427f58 100644 --- a/d3DicHandlerInterface.php +++ b/d3DicHandlerInterface.php @@ -15,10 +15,12 @@ declare(strict_types=1); namespace D3\DIContainerHandler; +use Symfony\Component\DependencyInjection\Container; + /** * Interface d3DicHandlerInterface */ interface d3DicHandlerInterface { - public static function getInstance(); + public static function getInstance(): ?Container; } \ No newline at end of file diff --git a/d3DicUtilities.php b/d3DicUtilities.php index 2bbbba6..b3c7155 100644 --- a/d3DicUtilities.php +++ b/d3DicUtilities.php @@ -18,11 +18,12 @@ namespace D3\DIContainerHandler; class d3DicUtilities { /** - * @param $classNameSpace - * @param bool $additional + * @param string $classNameSpace + * @param string|null $additional + * * @return string */ - public static function getServiceId($classNameSpace, $additional = false) + public static function getServiceId(string $classNameSpace, ?string $additional = null): string { return strtolower( ($additional ? $additional.'.' : ''). @@ -31,11 +32,12 @@ class d3DicUtilities } /** - * @param $classNameSpace - * @param $argumentName + * @param string $classNamespace + * @param string $argumentName + * * @return string */ - public static function getArgumentId($classNamespace, $argumentName) + public static function getArgumentId(string $classNamespace, string $argumentName): string { return strtolower( $classNamespace. diff --git a/definitionFileContainer.php b/definitionFileContainer.php index 45452b1..f9761b1 100644 --- a/definitionFileContainer.php +++ b/definitionFileContainer.php @@ -15,15 +15,17 @@ declare(strict_types=1); namespace D3\DIContainerHandler; +use InvalidArgumentException; + class definitionFileContainer { public const TYPE_YAML = 'yml'; - protected $definitionFiles = [ + protected array $definitionFiles = [ self::TYPE_YAML => [] ]; - protected $allowedTypes = [ + protected array $allowedTypes = [ self::TYPE_YAML ]; @@ -32,30 +34,49 @@ class definitionFileContainer $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)) { - throw new \InvalidArgumentException('invalid definition file type'); + throw new InvalidArgumentException( 'invalid definition file type'); } $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); } - public function getDefinitions($type) + /** + * @param string $type + * + * @return array + */ + public function getDefinitions(string $type): array { if (!in_array($type, $this->allowedTypes)) { - throw new \InvalidArgumentException('invalid definition file type'); + throw new InvalidArgumentException( 'invalid definition file type'); } return $this->definitionFiles[$type]; } - public function getYamlDefinitions() + /** + * @return array + */ + public function getYamlDefinitions(): array { return $this->getDefinitions(self::TYPE_YAML); } @@ -64,17 +85,17 @@ class definitionFileContainer * @param string $definitionFile * @return bool */ - public function has($definitionFile): bool + public function has(string $definitionFile): bool { return isset($this->definitionFiles[md5($definitionFile)]); } - public function getAll() + public function getAll(): array { return $this->definitionFiles; } - public function clear() + public function clear(): void { $this->definitionFiles = []; } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..ddb1a12 --- /dev/null +++ b/phpstan.neon @@ -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 \ No newline at end of file