diff --git a/.gitignore b/.gitignore index b7df245..91ce8c6 100644 --- a/.gitignore +++ b/.gitignore @@ -172,9 +172,11 @@ vendor/ .php-cs-fixer.cache ### PHPUnit template -# Generated files .phpunit.result.cache -.phpunit.cache +.phpunit.cache/ + +### PHP Mess Detector +.phpmd.result-cache.php ### Custom diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index aa7ecf2..9a0858c 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -2,10 +2,10 @@ declare(strict_types=1); -$headerProjName = 'ezkoding'; +$headerProjName = 'ezkoding'; $headerProjAuthor = 'Oliver Glowa, coding.glowa.com'; -$headerProjYear = 2025; -$headerText = << true, - '@PSR12' => true, + '@PSR2' => true, + '@PSR12' => true, + '@PHP8x4Migration' => true, // Customizing - 'declare_strict_types' => true, - 'no_trailing_comma_in_singleline' => true, - 'no_unused_imports' => true, - 'protected_to_private' => false, - 'modifier_keywords' => true, - 'return_assignment' => true, + 'declare_strict_types' => true, + 'no_trailing_comma_in_singleline' => true, + 'no_unused_imports' => true, + 'protected_to_private' => false, + 'modifier_keywords' => true, + 'return_assignment' => true, // Blank lines / spacing - 'class_attributes_separation' => [ + 'class_attributes_separation' => [ 'elements' => [ - 'const' => 'one', - 'method' => 'one', - 'property' => 'one', + 'const' => 'one', + 'method' => 'one', + 'property' => 'one', 'trait_import' => 'none', - 'case' => 'none' + 'case' => 'none' ] ], - 'blank_line_before_statement' => [ + 'blank_line_before_statement' => [ 'statements' => [ 'declare', 'exit', @@ -48,7 +49,8 @@ 'try' ] ], - 'no_extra_blank_lines' => [ + 'no_unused_imports' => true, + 'no_extra_blank_lines' => [ 'tokens' => [ 'attribute', 'break', @@ -65,20 +67,29 @@ 'throw' ] ], - 'statement_indentation' => ['stick_comment_to_next_continuous_control_statement' => true], + 'ordered_imports' => [ + 'imports_order' => ['class', 'function', 'const'], + 'sort_algorithm' => 'alpha' + ], + 'single_import_per_statement' => [ + 'group_to_single_imports' => true + ], + 'statement_indentation' => ['stick_comment_to_next_continuous_control_statement' => true], // Comments - 'multiline_comment_opening_closing' => true, - 'no_empty_comment' => true, - 'single_line_comment_spacing' => true, - 'single_line_comment_style' => true, - 'align_multiline_comment' => ['comment_type' => 'phpdocs_like'], + 'multiline_comment_opening_closing' => true, + 'no_empty_comment' => true, + 'single_line_comment_spacing' => true, + 'single_line_comment_style' => true, + 'align_multiline_comment' => ['comment_type' => 'phpdocs_like'], // PHPDoc - 'no_blank_lines_after_phpdoc' => true, - 'no_empty_phpdoc' => true, - 'phpdoc_add_missing_param_annotation' => true, - 'phpdoc_align' => [ + 'no_blank_lines_after_phpdoc' => true, + 'no_empty_phpdoc' => true, + 'phpdoc_add_missing_param_annotation' => [ + 'only_untyped' => false, + ], + 'phpdoc_align' => [ 'align' => 'vertical', - 'tags' => [ + 'tags' => [ 'method', 'param', 'property', @@ -107,20 +118,21 @@ 'psalm-method' ] ], - 'phpdoc_annotation_without_dot' => true, - 'phpdoc_indent' => true, - 'phpdoc_inline_tag_normalizer' => true, - 'phpdoc_line_span' => ['property' => 'single', 'const' => 'single', 'method' => 'multi'], - 'phpdoc_no_access' => true, - 'phpdoc_no_alias_tag' => true, - 'phpdoc_no_empty_return' => true, - 'phpdoc_no_package' => true, - 'phpdoc_no_useless_inheritdoc' => true, - 'phpdoc_order' => ['order' => ['param', 'return', 'throws']], - 'phpdoc_param_order' => true, - 'phpdoc_return_self_reference' => true, - 'phpdoc_scalar' => true, - 'phpdoc_separation' => [ + 'phpdoc_annotation_without_dot' => true, + 'phpdoc_indent' => true, + 'phpdoc_inline_tag_normalizer' => true, + 'phpdoc_line_span' => ['property' => 'single', 'const' => 'single', 'method' => 'multi'], + 'phpdoc_no_access' => true, + 'phpdoc_no_alias_tag' => true, + 'phpdoc_no_duplicate_types' => true, + 'phpdoc_no_empty_return' => true, + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_order' => ['order' => ['param', 'return', 'throws']], + 'phpdoc_param_order' => true, + 'phpdoc_return_self_reference' => true, + 'phpdoc_scalar' => true, + 'phpdoc_separation' => [ 'groups' => [ ['author', 'copyright', 'license'], ['category', 'package', 'subpackage'], @@ -129,17 +141,26 @@ ['psalm-suppress', 'phpstan-ignore'] ], ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_summary' => true, - 'phpdoc_tag_type' => true, - 'phpdoc_to_comment' => ['ignored_tags' => ['phpstan-ignore', 'psalm-suppress']], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_summary' => true, + 'phpdoc_tag_casing' => true, + 'phpdoc_tag_type' => true, + 'phpdoc_to_comment' => ['ignored_tags' => ['phpstan-ignore', 'psalm-suppress']], 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'phpdoc_trim' => true, - 'phpdoc_types' => true, - 'phpdoc_types_order' => true, - 'phpdoc_var_annotation_correct_order' => true, - 'phpdoc_var_without_name' => true, - 'header_comment' => ['header' => $headerText] + 'phpdoc_trim' => true, + 'phpdoc_types' => true, + 'phpdoc_types_order' => true, + 'phpdoc_var_annotation_correct_order' => true, + 'phpdoc_var_without_name' => true, + // Risky + /* + '@PHP8x2Migration:risky' => true, + 'phpdoc_to_param_type' =>true, + 'phpdoc_to_property_type' =>true, + 'phpdoc_to_return_type' => true, + */ + // Header + 'header_comment' => ['header' => $headerText] ]; $finder = PhpCsFixer\Finder::create()->in(__DIR__ . '/src')->in(__DIR__ . '/tests'); diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index e5d8086..7fb292c 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -22,14 +22,13 @@ /vendor/autoload.php + - - - - + + @@ -38,6 +37,25 @@ + + + + + + + + + + + + + + + + + + + tests/**/*Test\.php @@ -46,6 +64,20 @@ + + + + + + + + + + + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 851a53c..7cc8017 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,15 @@ - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" + bootstrap="tests/bootstrap.php" + cacheDirectory=".phpunit.cache" + colors="true" + executionOrder="depends,defects" + beStrictAboutOutputDuringTests="false" + displayDetailsOnTestsThatTriggerDeprecations="true" + displayDetailsOnPhpunitDeprecations="true" +> @@ -18,23 +22,14 @@ + + + ./src + + + - - - - - - - - - src - - - + diff --git a/psalm.xml.dist b/psalm.xml.dist index 1418af2..8ba8fbb 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -1,6 +1,6 @@ + - + - - + diff --git a/src/Monolog/EasyGoingLogger.php b/src/Monolog/EasyGoingLogger.php index b2194c7..8d51ed4 100644 --- a/src/Monolog/EasyGoingLogger.php +++ b/src/Monolog/EasyGoingLogger.php @@ -20,11 +20,11 @@ class EasyGoingLogger { - private const LOGGER_DEFAULT = 'Monolog\Logger'; + private const string LOGGER_DEFAULT = 'Monolog\Logger'; - private const LOGGER_CONSOLE = 'Monolog\ConsoleLogger'; + private const string LOGGER_CONSOLE = 'Monolog\ConsoleLogger'; - private const LOGGER_NULL = 'Psr\Log\NullLogger'; + private const string LOGGER_NULL = 'Psr\Log\NullLogger'; private function __construct() { @@ -32,19 +32,17 @@ private function __construct() } /** - * @param string $name - * @param string $level - * @param array $handlers - * @param array $processors - * @param null|DateTimeZone $timezone - * - * @return LoggerInterface + * @param string $name + * @param string $level + * @param array $handlers + * @param array $processors + * @param ?DateTimeZone $timezone * * @psalm-suppress InvalidNullableReturnType */ public static function init( string $name, - $level = LogLevel::INFO, + string $level = LogLevel::INFO, array $handlers = [], array $processors = [], ?DateTimeZone $timezone = null diff --git a/src/PHPUnit/Framework/ConstantCheckTestCase.php b/src/PHPUnit/Framework/ConstantCheckTestCase.php index 97d4613..64d0dde 100644 --- a/src/PHPUnit/Framework/ConstantCheckTestCase.php +++ b/src/PHPUnit/Framework/ConstantCheckTestCase.php @@ -18,11 +18,11 @@ abstract class ConstantCheckTestCase extends EasyGoingTestCase { - public const DIFF_ZERO = 0; + public const int DIFF_ZERO = 0; - public const INIT_CONST_COUNT = 0; + public const int INIT_CONST_COUNT = 0; - public const INIT_CROSSCHECK = false; + public const bool INIT_CROSSCHECK = false; /** * @var bool TRUE=Execute a constants cross check (Default: FALSE) @@ -30,7 +30,7 @@ abstract class ConstantCheckTestCase extends EasyGoingTestCase * @see ConstantCheckTestCase::expectedConstsCount * @see ConstantCheckTestCase::CHECK_INIT */ - private static $withConstCrossCheck = self::INIT_CROSSCHECK; + private static bool $withConstCrossCheck = self::INIT_CROSSCHECK; /** * @var int Set the expected and correct count of constants in child classes. Only used if {@link ConstantCheckTestCase::$withConstCrossCheck}=true. @@ -38,23 +38,20 @@ abstract class ConstantCheckTestCase extends EasyGoingTestCase * @see ConstantCheckTestCase::$withConstCrossCheck * @see ConstantCheckTestCase::testAllConstants() */ - private static $expectedConstsCount = self::INIT_CONST_COUNT; + private static int $expectedConstsCount = self::INIT_CONST_COUNT; - /** @var mixed[] Array of the names of all constants in the class. */ - private static $actualConsts = []; + /** @var array Array of the names of all constants in the class. */ + private static array $actualConsts = []; - /** @var LoggerInterface */ - private static $logger; + private static LoggerInterface $logger; /** * Inits the constants crosscheck. * - * @param bool $withConstCrossCheck - * @param int $expectedConstsCount - * * @see ConstantCheckTestCase::$withConstCrossCheck * @see ConstantCheckTestCase::$expectedConstsCount */ + #[\Override] public static function setUpBeforeClass(bool $withConstCrossCheck = self::INIT_CROSSCHECK, int $expectedConstsCount = self::INIT_CONST_COUNT): void { self::$logger = EasyGoingLogger::init(ConstantCheckTestCase::class); @@ -73,6 +70,7 @@ public static function setUpBeforeClass(bool $withConstCrossCheck = self::INIT_C /** * Performs the constants crosscheck at the end. */ + #[\Override] public static function tearDownAfterClass(): void { self::$logger->debug('START'); @@ -99,12 +97,12 @@ public static function isWithConstCrossCheck(): bool /** * Executes the constant crosscheck and fails if a constant is not found or not expected to exist. * - * @param mixed $clazz the clazz having the constants to check - * @param mixed[] $actualConsts an array of the already found constants + * @param mixed $clazz the clazz having the constants to check + * @param array $actualConsts an array of the already found constants * * @see ConstantCheckTestCase::$withConstCrossCheck */ - protected static function crossCheckConstants($clazz, $actualConsts): void + protected static function crossCheckConstants(mixed $clazz, array $actualConsts): void { self::$logger->debug('START'); @@ -114,12 +112,8 @@ protected static function crossCheckConstants($clazz, $actualConsts): void ksort($expected); $expected = array_keys($expected); - $callback = /** - * @param mixed $value - * - * @return string - */ - function ($value): string { + $callback = + function (mixed $value): string { $res = ''; if (is_string($value) && str_contains($value, self::C_STATIC_SEP)) { try { @@ -136,7 +130,6 @@ function ($value): string { }; self::$logger->info(' * Remove clazz prefix'); - /** @var string[] */ $actual = array_map($callback, $actualConsts); self::$logger->info(' * Flip constants'); $actual = array_flip($actual); @@ -154,7 +147,7 @@ function ($value): string { self::assertCount( self::DIFF_ZERO, $difference, - 'You have forgotten to check: ' . print_r(array_diff($expected, $actual), true) + 'You have forgotten to check: ' . var_export(array_diff($expected, $actual), true) ); } @@ -164,11 +157,11 @@ function ($value): string { /** * Adds an array of constants which have been found. * - * @param null|mixed[] $checkedConsts array of found constants + * @param null|array $checkedConsts array of found constants * * @see ConstantCheckTestCase::$withConstCrossCheck */ - protected static function updateActualConsts($checkedConsts): void + protected static function updateActualConsts(?array $checkedConsts): void { if (self::$withConstCrossCheck && !is_null($checkedConsts)) { self::$actualConsts = array_merge(self::$actualConsts, $checkedConsts); @@ -178,14 +171,14 @@ protected static function updateActualConsts($checkedConsts): void /** * Checks, if {@link $allDefinedConsts) has the size of {@link $expectedCount}. * - * @param int $expectedCount count of constants which must exists - * @param mixed[] $allDefinedConsts an array with all defined constants + * @param int $expectedCount count of constants which must exists + * @param array $allDefinedConsts an array with all defined constants * * @return array [true|false, count($allDefinedConsts)] * * @see ConstantCheckTestCase::$withConstCrossCheck */ - protected static function checkConstantsCount(int $expectedCount, $allDefinedConsts) + protected static function checkConstantsCount(int $expectedCount, array $allDefinedConsts): array { self::$logger->debug('START'); @@ -230,7 +223,7 @@ public function testAllConstants(): void * Checks, if all constants exists. * ['CONST1','CONST2',...]. * - * @param mixed[] $constants an array with constants to check + * @param array $constants an array with constants to check */ protected function verifyConstAllExists(array $constants = []): void { @@ -247,7 +240,7 @@ protected function verifyConstAllExists(array $constants = []): void * Checks, if all constants exists are arrays and have the expected size. * ['CONST1'=>3,'CONST2'=>10,...]. * - * @param mixed[] $constants an array with constants and expected sizes to check + * @param array $constants an array with constants and expected sizes to check */ protected function verifyConstArrayAllExists(array $constants = []): void { diff --git a/src/PHPUnit/Framework/EasyGoingTestCase.php b/src/PHPUnit/Framework/EasyGoingTestCase.php index 60ddb36..7b53caa 100644 --- a/src/PHPUnit/Framework/EasyGoingTestCase.php +++ b/src/PHPUnit/Framework/EasyGoingTestCase.php @@ -19,49 +19,45 @@ abstract class EasyGoingTestCase extends TestCase { /** var string Separator for static access */ - public const C_STATIC_SEP = '::'; + public const string C_STATIC_SEP = '::'; /** @var string All primitive datatypes */ - protected const C_PRIMITIVES = 'int|integer|bool|boolean|float'; + protected const string C_PRIMITIVES = 'int|integer|bool|boolean|float'; - /** @var LoggerInterface */ - private static $logger; + private static LoggerInterface $logger; /** @var mixed The object which will be tested. */ - protected $o2t; + protected mixed $o2t; /** - * @return mixed + * @return mixed new created instance */ - abstract protected static function prepareO2t(); + abstract protected static function prepareO2t(): mixed; /** - * @return mixed + * @return mixed current instance */ - abstract protected function getCasto2t(); + abstract protected function getCasto2t(): mixed; - /** - * @param mixed $name - * @param mixed[] $data - * @param string $dataName - */ - public function __construct($name = null, $data = [], $dataName = '') + #[\Override] + public static function setUpBeforeClass(): void { self::$logger = EasyGoingLogger::init(EasyGoingTestCase::class); self::$logger->debug('START'); - parent::__construct($name, $data, $dataName); - - $testInfo = [$this->get_called_function(), self::get_called_clazz()]; - self::$logger->debug('calledFunction,calledClazz', $testInfo); + parent::setUpBeforeClass(); self::$logger->debug('END'); } + #[\Override] public function setUp(): void { self::$logger->debug('START'); + $testInfo = [$this->get_called_function(), self::get_called_clazz()]; + self::$logger->debug('calledFunction,calledClazz', $testInfo); + parent::setUp(); $this->o2t = static::prepareO2t(); @@ -88,12 +84,7 @@ protected static function get_called_clazz(): string return $calledClazz; } - /** - * @param mixed $var - * - * @return bool - */ - protected static function isPrimitive($var): bool + protected static function isPrimitive(mixed $var): bool { $primitive = false; @@ -105,24 +96,18 @@ protected static function isPrimitive($var): bool } /** - * @param mixed $clazz + * @param mixed $clazzName * - * @return mixed[] + * @return array */ - protected static function getAllDefinedConsts($clazz): array + protected static function getAllDefinedConsts(mixed $clazzName): array { - $clazz = new \ReflectionClass($clazz); + $instance = new \ReflectionClass($clazzName); - return $clazz->getConstants(); // NOSONAR php:S3011 + return $instance->getConstants(); // NOSONAR php:S3011 } - /** - * @param mixed $clazz - * @param string $constantName - * - * @return bool - */ - protected static function isConstExist($clazz, string $constantName): bool + protected static function isConstExist(mixed $clazz, string $constantName): bool { self::$logger->debug('START'); @@ -160,13 +145,7 @@ public function testInit(): void // Misc functions - /** - * @param mixed $clazz - * @param string $constantName - * - * @return mixed - */ - protected static function getConstValue($clazz, string $constantName) + protected static function getConstValue(mixed $clazz, string $constantName): mixed { self::$logger->debug('START'); @@ -188,11 +167,6 @@ protected static function getConstValue($clazz, string $constantName) return $constantValue; } - /** - * @param string $constantName - * - * @SuppressWarnings("PHPMD.ElseExpression") - */ protected function verifyConstExists(string $constantName): void { self::$logger->debug('START'); diff --git a/src/Tools/Batch/BatchTaskHelper.php b/src/Tools/Batch/BatchTaskHelper.php index acb5c37..ec4609c 100644 --- a/src/Tools/Batch/BatchTaskHelper.php +++ b/src/Tools/Batch/BatchTaskHelper.php @@ -20,13 +20,12 @@ class BatchTaskHelper { /** Default key for a tasklist. */ - public const DEFAULT = 'DEFAULT'; + public const string DEFAULT = 'DEFAULT'; - /** @var \Ds\Map */ - private static $tasklists; + /** @var Map */ + private static ?Map $tasklists = null; - /** @var LoggerInterface */ - private static $logger; + private static ?LoggerInterface $logger = null; private function __construct() { @@ -35,65 +34,72 @@ private function __construct() public static function init(): void { - /** - * @psalm-suppress DocblockTypeContradiction - * @phpstan-ignore function.impossibleType - */ if (is_null(self::$logger)) { self::$logger = EasyGoingLogger::init(BatchTaskHelper::class); } - /** - * @psalm-suppress DocblockTypeContradiction - * @phpstan-ignore function.impossibleType - */ if (is_null(self::$tasklists)) { self::$tasklists = new Map(); } } /** - * @param null|string $listKey + * @return LoggerInterface * - * @return TaskList + * @psalm-suppress InvalidNullableReturnType */ - public static function getTaskList(?string $listKey): TaskList + private static function logger(): LoggerInterface { - self::init(); + /** + * @psalm-suppress NullableReturnStatement + * @phpstan-ignore return.type + */ + return self::$logger; + } + + /** + * @return Map + * + * @psalm-suppress InvalidNullableReturnType + */ + private static function taskLists(): Map + { + /** + * @psalm-suppress NullableReturnStatement + * @phpstan-ignore return.type + */ + return self::$tasklists; + } - self::$logger->debug('START - listKey', [$listKey]); + public static function getTaskList(string $listKey = self::DEFAULT): TaskList + { + self::init(); + self::logger()->debug('START - listKey', [$listKey]); - $listKey = $listKey ?? self::DEFAULT; - if (!self::$tasklists->hasKey($listKey)) { - self::$tasklists->put($listKey, new TaskList($listKey)); + $listKey = empty($listKey) ? self::DEFAULT : $listKey; + if (!self::taskLists()->hasKey($listKey)) { + self::taskLists()->put($listKey, new TaskList($listKey)); } - self::$logger->debug('END'); + self::logger()->debug('END'); - return self::$tasklists->get($listKey); + return self::taskLists()->get($listKey); } - /** - * @param string $fileName - * @param null|string $listKey - * - * @return TaskList - */ - public static function readTaskList(string $fileName, ?string $listKey) + public static function readTaskList(string $fileName, string $listKey = self::DEFAULT): TaskList { self::init(); + self::logger()->debug('START - listKey,fileName', [$listKey, $fileName]); - self::$logger->debug('START - listKey,fileName', [$listKey, $fileName]); - - $listKey = $listKey ?? self::DEFAULT; + $listKey = empty($listKey) ? self::DEFAULT : $listKey; if (file_exists($fileName)) { $taskList = self::getTaskList($listKey); $taskList->readFile($fileName); } else { - self::$logger->warning('File does not exists!', [$fileName]); + self::logger()->warning('File does not exists!', [$fileName]); $taskList = self::getTaskList($listKey); } - self::$logger->debug('END'); + self::logger()->debug('END'); return $taskList; } diff --git a/src/Tools/Batch/ITaskItem.php b/src/Tools/Batch/ITaskItem.php index bd56c07..cf5816d 100644 --- a/src/Tools/Batch/ITaskItem.php +++ b/src/Tools/Batch/ITaskItem.php @@ -20,21 +20,14 @@ interface ITaskItem { /** - * @return mixed - * * @phpstan-return TaskKey */ - public function getKey(); + public function getKey(): mixed; /** - * @return array - * * @phpstan-return TaskData */ public function getData(): array; - /** - * @return mixed - */ - public function __toString(); + public function __toString(): string; } diff --git a/src/Tools/Batch/TaskItem.php b/src/Tools/Batch/TaskItem.php index c712ffa..d1e4e07 100644 --- a/src/Tools/Batch/TaskItem.php +++ b/src/Tools/Batch/TaskItem.php @@ -23,52 +23,39 @@ class TaskItem implements ITaskItem { use ToStringTrait; - /** - * @var mixed - * - * @phpstan-var TaskKey */ - private $key; + /** @phpstan-var TaskKey */ + private mixed $key; - /** - * @var array - * - * @phpstan-var TaskData - */ - private $data; + /** @phpstan-var TaskData */ + private array $data; /** - * @param mixed $key - * @param array $data + * @param mixed $key + * @param array $data * * @phpstan-param TaskKey $key * @phpstan-param TaskData $data */ - public function __construct($key, array $data) + public function __construct(mixed $key, array $data) { $this->key = $key; $this->data = $data; } - /** - * @inheritDoc - */ - public function getKey() + #[\Override] + public function getKey(): mixed { return $this->key; } - /** - * @inheritDoc - */ + #[\Override] public function getData(): array { return $this->data; } - /** - * @return mixed - */ - protected function __toStringValues() // NOSONAR: php:S100 + #[\Override] + protected function __toStringValues(): mixed // NOSONAR: php:S100 { return $this->data; } diff --git a/src/Tools/Batch/TaskList.php b/src/Tools/Batch/TaskList.php index 143c490..d0824ab 100644 --- a/src/Tools/Batch/TaskList.php +++ b/src/Tools/Batch/TaskList.php @@ -19,14 +19,12 @@ class TaskList { - /** @var LoggerInterface */ - private static $logger; + private static LoggerInterface $logger; - /** @var string */ - private $listKey; + private string $listKey; /** @var Queue */ - private $tasks; + private Queue $tasks; public function __construct(string $listKey) { @@ -45,9 +43,6 @@ public function addTask(ITaskItem $task): void $this->tasks->push($task); } - /** - * @return null|ITaskItem - */ public function nextTask(): ?ITaskItem { $task = null; @@ -68,11 +63,6 @@ public function isEmpty(): bool return $this->tasks->isEmpty(); } - /** - * @param string $fileName - * - * @return bool - */ public function readFile(string $fileName): bool { self::$logger->debug('START - fileName', [$fileName]); @@ -86,7 +76,6 @@ public function readFile(string $fileName): bool while ($line = fgets($fHandle, 1000)) { $convertedLine = mb_convert_encoding($line, 'UTF-8'); $itemKey = $this->listKey . $idx; - /** @var null|ITaskItem $newTask */ $newTask = $this->parseTaskData($itemKey, $convertedLine); if (!is_null($newTask)) { $this->addTask($newTask); @@ -102,17 +91,10 @@ public function readFile(string $fileName): bool return $fileRead; } - /** - * @param mixed $itemKey - * @param mixed $convertedLine - * - * @return null|ITaskItem - */ - protected function parseTaskData($itemKey, $convertedLine): ?ITaskItem + protected function parseTaskData(mixed $itemKey, mixed $convertedLine): ?ITaskItem { self::$logger->debug('START - itemKey', [$itemKey]); - /** @var null|ITaskItem $newTask */ $newTask = null; if (is_string($convertedLine)) { $newLine = preg_filter("/(\r|\n|\r\n)/", '', $convertedLine); @@ -128,11 +110,6 @@ protected function parseTaskData($itemKey, $convertedLine): ?ITaskItem return $newTask; } - /** - * @param string $fileName - * - * @return bool - */ public function storeFile(string $fileName): bool { self::$logger->debug('START - fileName', [$fileName]); @@ -147,8 +124,12 @@ public function storeFile(string $fileName): bool if ($line instanceof ITaskItem) { $line = $line->__toString(); } - $convertedLine = mb_convert_encoding($line, 'UTF-8') . "\n"; - fwrite($fHandle, $convertedLine); + $convertedLine = mb_convert_encoding($line, 'UTF-8'); + /** @phpstan-ignore notIdentical.alwaysTrue */ + if ($convertedLine !== false) { + $convertedLine .= "\n"; + fwrite($fHandle, $convertedLine); + } } fclose($fHandle); $fileStored = true; diff --git a/src/Tools/Emergency.php b/src/Tools/Emergency.php index 793c16f..7d2d2e3 100644 --- a/src/Tools/Emergency.php +++ b/src/Tools/Emergency.php @@ -19,13 +19,13 @@ class Emergency { /** Default message */ - public const MSG_DEFAULT = 'Undefined reason to stop now!'; + public const string MSG_DEFAULT = 'Undefined reason to stop now!'; /** Default code */ - public const ERR_CODE_DEFAULT = 1; + public const int ERR_CODE_DEFAULT = 1; /** Maximum code which is allowed. */ - private const ERR_CODE_MAX = 254; + private const int ERR_CODE_MAX = 254; /** * Immediately stopping the application caused by an exception. As a hmomage to "Knight Rider". @@ -39,7 +39,7 @@ class Emergency */ public static function exceptionStop(\Throwable $throwable, bool $unitTest = false): int { - $errMsg = sprintf('\%s - %s', get_class($throwable), $throwable->getMessage()); + $errMsg = sprintf('\%s - %s', $throwable::class, $throwable->getMessage()); /** @psalm-suppress PossiblyInvalidArgument */ return static::breakSystem($throwable->getCode(), $errMsg, $unitTest); diff --git a/src/Tools/EnvironmentVariableTrait.php b/src/Tools/EnvironmentVariableTrait.php index 9f4c544..5be09e4 100644 --- a/src/Tools/EnvironmentVariableTrait.php +++ b/src/Tools/EnvironmentVariableTrait.php @@ -20,11 +20,6 @@ */ trait EnvironmentVariableTrait { - /** - * @param string $homeVariable - * - * @return string - */ final public static function getHome(string $homeVariable = 'HOME'): string { $home = getenv($homeVariable); @@ -40,9 +35,6 @@ final public static function getHome(string $homeVariable = 'HOME'): string return $home; } - /** - * @return string - */ final public static function getProjectRoot(): string { $projectRoot = self::getComposerFilePath(); @@ -53,9 +45,6 @@ final public static function getProjectRoot(): string return (string)realpath($projectRoot); } - /** - * @return string - */ private static function getComposerFilePath(): string { $composerFile = Factory::getComposerFile(); @@ -67,11 +56,6 @@ private static function getComposerFilePath(): string return $composerPath; } - /** - * @param int $folderOffset - * - * @return string - */ private static function getProjectRootFallback(int $folderOffset = 2): string { $rootClazz = new \ReflectionClass(EnvironmentVariableTrait::class); diff --git a/src/Tools/Reflection/ChildClazzesHelper.php b/src/Tools/Reflection/ChildClazzesHelper.php index 873aa4e..21e1ceb 100644 --- a/src/Tools/Reflection/ChildClazzesHelper.php +++ b/src/Tools/Reflection/ChildClazzesHelper.php @@ -13,7 +13,7 @@ namespace ollily\Tools\Reflection; -final class ChildClazzesHelper +class ChildClazzesHelper { private function __construct() { @@ -27,7 +27,7 @@ private function __construct() * * @return string[] */ - public static function getAllChildren($clazzName): array + public static function getAllChildren(mixed $clazzName): array { $children = []; foreach (get_declared_classes() as $currentClazz) { diff --git a/src/Tools/Reflection/UnavailableFieldsTrait.php b/src/Tools/Reflection/UnavailableFieldsTrait.php index b02697f..fa17bcf 100644 --- a/src/Tools/Reflection/UnavailableFieldsTrait.php +++ b/src/Tools/Reflection/UnavailableFieldsTrait.php @@ -17,14 +17,7 @@ trait UnavailableFieldsTrait { - /** - * @param mixed $clazzName - * @param string $fieldName - * @param mixed $instance - * - * @return null|mixed - */ - protected function getFieldByReflection($clazzName, string $fieldName, $instance) + protected function getFieldByReflection(mixed $clazzName, string $fieldName, mixed $instance): mixed { $result = null; if (!empty($clazzName)) { @@ -37,13 +30,7 @@ protected function getFieldByReflection($clazzName, string $fieldName, $instance return $result; } - /** - * @param mixed $clazzName - * @param string $fieldName - * @param mixed $instance - * @param null|mixed $newValue - */ - protected function setFieldByReflection($clazzName, string $fieldName, $instance, $newValue): void + protected function setFieldByReflection(mixed $clazzName, string $fieldName, mixed $instance, mixed $newValue): void { if (!empty($clazzName)) { $refObject = new ReflectionProperty($clazzName, $fieldName); @@ -53,17 +40,12 @@ protected function setFieldByReflection($clazzName, string $fieldName, $instance } } - /** - * @param string $fieldName - * - * @return null|mixed - */ - protected function getFieldFromO2t(string $fieldName) + protected function getFieldFromO2t(string $fieldName): mixed { $result = null; /** - * @psalm-suppress RedundantPropertyInitializationCheck + * @psalm-suppress RedundantPropertyInitializationCheck,UndefinedThisPropertyFetch * @phpstan-ignore isset.property,property.notFound */ if (isset($this->o2t)) { diff --git a/src/Tools/Reflection/UnavailableMethodsTrait.php b/src/Tools/Reflection/UnavailableMethodsTrait.php index 51ae4aa..6db52ab 100644 --- a/src/Tools/Reflection/UnavailableMethodsTrait.php +++ b/src/Tools/Reflection/UnavailableMethodsTrait.php @@ -23,10 +23,8 @@ trait UnavailableMethodsTrait * @param mixed $clazzName * @param string $methodName * @param mixed $instance - * - * @return null|mixed */ - protected function callMethodByReflection($clazzName, string $methodName, $instance) + protected function callMethodByReflection(mixed $clazzName, string $methodName, mixed $instance): mixed { $result = null; if (!empty($clazzName)) { @@ -43,15 +41,13 @@ protected function callMethodByReflection($clazzName, string $methodName, $insta * Calls a hidden method on an object which shall be tested (o2t). * * @param string $methodName - * - * @return null|mixed */ - protected function callMethodOnO2t(string $methodName) + protected function callMethodOnO2t(string $methodName): mixed { $result = null; /** - * @psalm-suppress RedundantPropertyInitializationCheck + * @psalm-suppress RedundantPropertyInitializationCheck,UndefinedThisPropertyFetch * @phpstan-ignore isset.property,property.notFound */ if (isset($this->o2t)) { diff --git a/src/Tools/String/ImplodeTrait.php b/src/Tools/String/ImplodeTrait.php index dfd7fa6..0ad07bb 100644 --- a/src/Tools/String/ImplodeTrait.php +++ b/src/Tools/String/ImplodeTrait.php @@ -19,30 +19,20 @@ trait ImplodeTrait { /** - * @return string Default glue character + * Default glue character. * * @see ImplodeTrait::array_flatten() * @see ImplodeTrait::implode_recursive() - * - * TODO: Make it a constant with PHP 8 */ - final public function DEFAULT_GLUE(): string // NOSONAR: php:S100 - { - return ','; - } + public const string DEFAULT_GLUE = ','; /** - * @return string Default separate character for csv + * Default separate character for csv. * * @see ImplodeTrait::array_flatten() * @see ImplodeTrait::implode_recursive() - * - * TODO: Make it a constant with PHP 8 */ - final public function DEFAULT_ITEM_SEP(): string // NOSONAR: php:S100 - { - return ';'; - } + public const string DEFAULT_ITEM_SEP = ';'; /** * Recursively implodes an array with optional key inclusion. @@ -59,7 +49,7 @@ final public function DEFAULT_ITEM_SEP(): string // NOSONAR: php:S100 * @SuppressWarnings("PHPMD.CamelCaseMethodName") */ // @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps - final public function implode_recursive(string $glue, $anyData, bool $withTextSep = false, bool $withKeys = false): string // NOSONAR: php:S100 + final public static function implode_recursive(string $glue, mixed $anyData, bool $withTextSep = false, bool $withKeys = false): string // NOSONAR: php:S100 { $output = ''; $valueIdx = 0; @@ -72,7 +62,7 @@ final public function implode_recursive(string $glue, $anyData, bool $withTextSe $output .= ($valueIdx > 0 ? $glue : '') . $currKey; if (is_array($value)) { - $arrOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys); + $arrOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys); if (!empty($arrOutput)) { $output .= '[' . $arrOutput . ']'; } else { @@ -80,7 +70,7 @@ final public function implode_recursive(string $glue, $anyData, bool $withTextSe } } else { if (is_object($value)) { - $objOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys); + $objOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys); if (!empty($objOutput)) { $output .= '{' . $objOutput . '}'; } else { @@ -97,7 +87,7 @@ final public function implode_recursive(string $glue, $anyData, bool $withTextSe if ($anyData instanceof Stringable) { $output = $anyData->__toString(); } else { - $output = get_class($anyData); + $output = $anyData::class; } } else { $output = $anyData; diff --git a/src/Tools/String/ToStringTrait.php b/src/Tools/String/ToStringTrait.php index 238b2d9..aa9fb7d 100644 --- a/src/Tools/String/ToStringTrait.php +++ b/src/Tools/String/ToStringTrait.php @@ -18,16 +18,11 @@ trait ToStringTrait use ImplodeTrait; /** - * @return mixed - * * @SuppressWarnings("PHPMD.CamelCaseMethodName") */ - abstract protected function __toStringValues(); // NOSONAR: php:S100 + abstract protected function __toStringValues(): mixed; // NOSONAR: php:S100 - /** - * @inheritdoc - */ - public function __toString() + public function __toString(): string { $value = $this->__toStringValues(); if (is_string($value)) { @@ -40,7 +35,7 @@ public function __toString() $value[$arrayKey] = get_class($value[$arrayKey]); } } - $toString = sprintf('%s:[%s]', get_class($this), $this->implode_recursive(',', $value)); + $toString = sprintf('%s:[%s]', get_class($this), self::implode_recursive(',', $value)); } elseif (is_object($value)) { if ($this == $value) { $toString = sprintf('{%s}', print_r($value, true)); @@ -54,10 +49,7 @@ public function __toString() return $toString; } - /** - * @inheritdoc - */ - public function __wakeup() + public function __wakeup(): void { throw new \BadMethodCallException("Cannot unserialize singleton"); } diff --git a/src/Tools/Test/TestData.php b/src/Tools/Test/TestData.php index c1d6eb8..c90d8bd 100644 --- a/src/Tools/Test/TestData.php +++ b/src/Tools/Test/TestData.php @@ -24,133 +24,135 @@ class TestData // Unspecific test data // Single data - public const DATA_NUM1 = 11; + public const int DATA_NUM1 = 11; - public const DATA_NUM2 = 22; + public const int DATA_NUM2 = 22; - public const DATA_NUM3 = 33; + public const int DATA_NUM3 = 33; - public const DATA_NUM4 = 44; + public const int DATA_NUM4 = 44; - public const DATA_NUM5 = 55; + public const int DATA_NUM5 = 55; - public const DATA_ALPHA1 = 'DATA-ALPHA11'; + public const string DATA_ALPHA1 = 'DATA-ALPHA11'; - public const DATA_ALPHA2 = 'DATA-ALPHA22'; + public const string DATA_ALPHA2 = 'DATA-ALPHA22'; - public const DATA_ALPHA3 = 'DATA-ALPHA33'; + public const string DATA_ALPHA3 = 'DATA-ALPHA33'; - public const DATA_ALPHA4 = 'DATA-ALPHA44'; + public const string DATA_ALPHA4 = 'DATA-ALPHA44'; - public const DATA_ALPHA5 = 'DATA-ALPHA55'; + public const string DATA_ALPHA5 = 'DATA-ALPHA55'; - public const DATA_BOOL_T = true; + public const bool DATA_BOOL_T = true; - public const DATA_BOOL_F = false; + public const bool DATA_BOOL_F = false; - /** @var null|string */ - public const DATA_NULL = null; + public const ?object DATA_NULL = null; - public const DATA_EMPTY = ''; + public const string DATA_EMPTY = ''; - public const DATA_INVALID = 'INVALID'; + public const string DATA_INVALID = 'INVALID'; - public const DATA_NOTEXIST = 'NOT-EXIST'; + public const string DATA_NOTEXIST = 'NOT-EXIST'; // Misc Data - public const NOTEXIST_NAME = 'XXXName'; + public const string NOTEXIST_NAME = 'XXXName'; - public const NOTEXIST_ID = 99999; + public const int NOTEXIST_ID = 99999; - public const NOTEXIST_ARRAY = [self::NOTEXIST_ID => self::NOTEXIST_NAME]; + public const array NOTEXIST_ARRAY = [self::NOTEXIST_ID => self::NOTEXIST_NAME]; - public const NOTEXIST_CLAZZ = 'ollily/NotExistClazz'; + public const string NOTEXIST_CLAZZ = 'ollily/NotExistClazz'; - public const NOTEXIST_FILE = DIRECTORY_SEPARATOR . 'NotExistFile'; + public const string NOTEXIST_FILE = DIRECTORY_SEPARATOR . 'NotExistFile'; // Array Data - public const ARRAY_ITEM_SEP = ','; + public const string ARRAY_ITEM_SEP = ','; // Array keys - public const KEY_NUM1 = 1; + public const int KEY_NUM1 = 1; - public const KEY_NUM2 = 2; + public const int KEY_NUM2 = 2; - public const KEY_NUM3 = 3; + public const int KEY_NUM3 = 3; - public const KEY_NUM4 = 4; + public const int KEY_NUM4 = 4; - public const KEY_NUM5 = 5; + public const int KEY_NUM5 = 5; - public const KEY_ALPHA1 = 'KEY-ALPHA1'; + public const string KEY_ALPHA1 = 'KEY-ALPHA1'; - public const KEY_ALPHA2 = 'KEY-ALPHA2'; + public const string KEY_ALPHA2 = 'KEY-ALPHA2'; - public const KEY_ALPHA3 = 'KEY-ALPHA3'; + public const string KEY_ALPHA3 = 'KEY-ALPHA3'; + + public const ?object KEY_NULL = null; + + public const string KEY_EMPTY = ''; // Arrays complete - public const ARRAY_EMPTY = []; + public const array ARRAY_EMPTY = []; - /** @var null|array */ - public const ARRAY_NULL = null; + public const ?array ARRAY_NULL = null; // Arrays with alphanumeric data - public const ARRAY_ALPHA1 = [self::DATA_ALPHA1]; + public const array ARRAY_ALPHA1 = [self::DATA_ALPHA1]; - public const ARRAY_ALPHA2 = [self::DATA_ALPHA1, self::DATA_ALPHA2]; + public const array ARRAY_ALPHA2 = [self::DATA_ALPHA1, self::DATA_ALPHA2]; - public const ARRAY_ALPHA3 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3]; + public const array ARRAY_ALPHA3 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3]; - public const ARRAY_ALPHA4 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3, self::DATA_ALPHA4]; + public const array ARRAY_ALPHA4 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3, self::DATA_ALPHA4]; - public const ARRAY_ALPHA5 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3, self::DATA_ALPHA4, self::DATA_ALPHA5]; + public const array ARRAY_ALPHA5 = [self::DATA_ALPHA1, self::DATA_ALPHA2, self::DATA_ALPHA3, self::DATA_ALPHA4, self::DATA_ALPHA5]; // Arrays with numeric data - public const ARRAY_NUM1 = [self::DATA_NUM1]; + public const array ARRAY_NUM1 = [self::DATA_NUM1]; - public const ARRAY_NUM2 = [self::DATA_NUM1, self::DATA_NUM2]; + public const array ARRAY_NUM2 = [self::DATA_NUM1, self::DATA_NUM2]; - public const ARRAY_NUM3 = [self::DATA_NUM1, self::DATA_NUM2, self::DATA_NUM3]; + public const array ARRAY_NUM3 = [self::DATA_NUM1, self::DATA_NUM2, self::DATA_NUM3]; // Arrays with bool data - public const ARRAY_BOOL1 = [self::DATA_BOOL_T]; + public const array ARRAY_BOOL1 = [self::DATA_BOOL_T]; - public const ARRAY_BOOL2 = [self::DATA_BOOL_T, self::DATA_BOOL_F]; + public const array ARRAY_BOOL2 = [self::DATA_BOOL_T, self::DATA_BOOL_F]; // Arrays with alphanumeric key - public const ARRAY_ALPHA_KEY1 = [self::KEY_ALPHA1 => self::DATA_ALPHA1]; + public const array ARRAY_ALPHA_KEY1 = [self::KEY_ALPHA1 => self::DATA_ALPHA1]; - public const ARRAY_ALPHA_KEY2 = [ + public const array ARRAY_ALPHA_KEY2 = [ self::KEY_ALPHA1 => self::DATA_ALPHA1, - self::KEY_ALPHA2 => self::DATA_ALPHA2 + self::KEY_ALPHA2 => self::DATA_ALPHA2, ]; - public const ARRAY_ALPHA_KEY3 = [ + public const array ARRAY_ALPHA_KEY3 = [ self::KEY_ALPHA1 => self::DATA_ALPHA1, self::KEY_ALPHA2 => self::DATA_ALPHA2, - self::KEY_ALPHA3 => self::DATA_ALPHA3 + self::KEY_ALPHA3 => self::DATA_ALPHA3, ]; // Arrays with exlicit numeric key - public const ARRAY_NUM_KEY1 = [self::KEY_NUM1 => self::DATA_NUM1]; + public const array ARRAY_NUM_KEY1 = [self::KEY_NUM1 => self::DATA_NUM1]; - public const ARRAY_NUM_KEY2 = [ + public const array ARRAY_NUM_KEY2 = [ self::KEY_NUM1 => self::DATA_NUM1, - self::KEY_NUM2 => self::DATA_NUM2 + self::KEY_NUM2 => self::DATA_NUM2, ]; - public const ARRAY_NUM_KEY3 = [ + public const array ARRAY_NUM_KEY3 = [ self::KEY_NUM1 => self::DATA_NUM1, self::KEY_NUM2 => self::DATA_NUM2, - self::KEY_NUM3 => self::DATA_NUM3 + self::KEY_NUM3 => self::DATA_NUM3, ]; // Arrays with object data @@ -158,62 +160,59 @@ class TestData /** * @see self::C_ARRAY_OBJECT1() * - * @var array + * @var array */ - private static $ARRAY_OBJECT1 = []; // NOSONAR: php:S100 + private static array $ARRAY_OBJECT1 = []; // NOSONAR: php:S100 /** * @see self::C_ARRAY_OBJECT2() * - * @var array + * @var array */ - private static $ARRAY_OBJECT2 = []; // NOSONAR: php:S100 + private static array $ARRAY_OBJECT2 = []; // NOSONAR: php:S100 /** * @see self::C_ARRAY_OBJECT3() * - * @var array + * @var array */ - private static $ARRAY_OBJECT3 = []; // NOSONAR: php:S100 + private static array $ARRAY_OBJECT3 = []; // NOSONAR: php:S100 // Filesystem Data // Foldernames - /** @var null|string */ - public const FILE_FOLDERNAME_NULL = null; + public const ?object FILE_FOLDERNAME_NULL = null; - public const FILE_FOLDERNAME_EMPTY = ''; + public const string FILE_FOLDERNAME_EMPTY = ''; - public const FILE_FOLDERNAME = 'FOLDER-EXIST'; + public const string FILE_FOLDERNAME = 'FOLDER-EXIST'; // Filenames - /** @var null|string */ - public const FILE_FILENAME_NULL = null; + public const ?object FILE_FILENAME_NULL = null; - public const FILE_FILENAME_EMPTY = ''; + public const string FILE_FILENAME_EMPTY = ''; - public const FILE_FILENAME = 'FILE-EXIST'; + public const string FILE_FILENAME = 'FILE-EXIST'; - public const FILE_FILENAME_PREFIX = 'teda-'; + public const string FILE_FILENAME_PREFIX = 'teda-'; // File Extensions - /** @var null|string */ - public const FILE_EXT_NULL = null; + public const ?object FILE_EXT_NULL = null; - public const FILE_EXT_EMPTY = ''; + public const string FILE_EXT_EMPTY = ''; - public const FILE_EXT_NAME = 'EXT-EXIST'; + public const string FILE_EXT_NAME = 'EXT-EXIST'; - public const FILE_EXT_PHP = '.php'; + public const string FILE_EXT_PHP = '.php'; - public const FILE_EXT_TXT = '.txt'; + public const string FILE_EXT_TXT = '.txt'; - public const FILE_EXT_JSON = '.json'; + public const string FILE_EXT_JSON = '.json'; - public const FILE_EXT_CSV = '.csv'; + public const string FILE_EXT_CSV = '.csv'; // Static functions @@ -225,7 +224,7 @@ public static function DATA_OBJECT1(): TestDataFoo // NOSONAR: php:S100 } /** - * @return array + * @return array */ public static function ARRAY_OBJECT1(): array // NOSONAR: php:S100 { @@ -237,7 +236,7 @@ public static function ARRAY_OBJECT1(): array // NOSONAR: php:S100 } /** - * @return array + * @return array */ public static function ARRAY_OBJECT2(): array // NOSONAR: php:S100 { @@ -252,7 +251,7 @@ public static function ARRAY_OBJECT2(): array // NOSONAR: php:S100 } /** - * @return array + * @return array */ public static function ARRAY_OBJECT3(): array // NOSONAR: php:S100 { @@ -283,16 +282,21 @@ private function __construct() */ public static function prepareTempFile(string $prefix = self::FILE_FILENAME_PREFIX): string { - return tempnam(sys_get_temp_dir(), $prefix); + $tmpFile = tempnam(sys_get_temp_dir(), $prefix); + if ($tmpFile == false) { + $tmpFile = ''; + } + + return $tmpFile; } /** - * @param string $fileName Full filename for a temporary file + * @param null|string $fileName Full filename for a temporary file */ - public static function cleanupTempFile(string $fileName): void + public static function cleanupTempFile(?string $fileName): void { try { - if (!empty($fileName) && file_exists($fileName)) { + if (!is_null($fileName) && !empty($fileName) && file_exists($fileName)) { unlink($fileName); } } catch (\Throwable $ex) { diff --git a/src/Tools/Test/TestDataFoo.php b/src/Tools/Test/TestDataFoo.php index 16551a8..7b59392 100644 --- a/src/Tools/Test/TestDataFoo.php +++ b/src/Tools/Test/TestDataFoo.php @@ -15,36 +15,28 @@ use ollily\Tools\String\ToStringTrait; +/** + * A simple object usable as test object. + */ class TestDataFoo { use ToStringTrait; - /** @var null|mixed */ - private $fooValue; + private mixed $fooValue; - /** - * @param null|mixed $fooValue - * - * @return TestDataFoo - */ - public static function init($fooValue = null): TestDataFoo + public static function init(mixed $fooValue = null): TestDataFoo { return new TestDataFoo($fooValue); } - /** - * @param null|mixed $fooValue - */ - public function __construct($fooValue = null) + public function __construct(mixed $fooValue = null) { $this->fooValue = $fooValue; } - /** - * @return mixed - */ - protected function __toStringValues() + #[\Override] + protected function __toStringValues(): mixed { - return $this; + return $this->fooValue; } } diff --git a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseClazz.php b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseClazz.php index 82c79dd..a1b9de0 100644 --- a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseClazz.php +++ b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseClazz.php @@ -23,23 +23,26 @@ */ class ConstantCheckTestCaseClazz extends ConstantCheckTestCase // NOSONAR: php:S3360 { + #[\Override] + public static function setUpBeforeClass(bool $withConstCrossCheck = self::INIT_CROSSCHECK, int $expectedConstsCount = self::INIT_CONST_COUNT): void + { + parent::setUpBeforeClass($withConstCrossCheck, $expectedConstsCount); + } + + #[\Override] public static function tearDownAfterClass(): void { // Deactivate the check, will be called manually in testcase } - /** - * @return ConstantCheckTestCaseDummyClazz - */ - protected static function prepareO2t() + #[\Override] + protected static function prepareO2t(): ConstantCheckTestCaseDummyClazz { return new ConstantCheckTestCaseDummyClazz(); } - /** - * @return ConstantCheckTestCaseDummyClazz - */ - protected function getCasto2t() + #[\Override] + protected function getCasto2t(): ConstantCheckTestCaseDummyClazz { return $this->o2t; } @@ -47,10 +50,10 @@ protected function getCasto2t() // Override the visibility for the test cases /** - * @param mixed $clazz - * @param mixed $actualConstants + * @param mixed $clazz + * @param array $actualConstants */ - public static function publicCrossCheckConstants($clazz, $actualConstants): void + public static function publicCrossCheckConstants(mixed $clazz, array $actualConstants): void { parent::crossCheckConstants($clazz, $actualConstants); } @@ -58,7 +61,7 @@ public static function publicCrossCheckConstants($clazz, $actualConstants): void /** * @param null|array $checkedConsts */ - public static function publicUpdateActualConsts($checkedConsts): void + public static function publicUpdateActualConsts(?array $checkedConsts): void { parent::updateActualConsts($checkedConsts); } @@ -67,9 +70,9 @@ public static function publicUpdateActualConsts($checkedConsts): void * @param int $expectedCount * @param array $allDefinedConsts * - * @return array + * @return array */ - public static function publicCheckConstantsCount(int $expectedCount, $allDefinedConsts) + public static function publicCheckConstantsCount(int $expectedCount, array $allDefinedConsts): array { return parent::checkConstantsCount($expectedCount, $allDefinedConsts); } @@ -90,10 +93,6 @@ public function publicVerifyConstArrayAllExists(array $constants = []): void parent::verifyConstArrayAllExists($constants); } - /** - * @param string $constantName - * @param int $expectedSize - */ public function publicVerifyConstArraySize(string $constantName, int $expectedSize): void { parent::verifyConstArraySize($constantName, $expectedSize); diff --git a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseDummyClazz.php b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseDummyClazz.php index 6c57e24..5ce1a55 100644 --- a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseDummyClazz.php +++ b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseDummyClazz.php @@ -20,15 +20,15 @@ */ class ConstantCheckTestCaseDummyClazz { - public const TEST_CLAZZ = self::class . '::'; + public const string TEST_CLAZZ = self::class . '::'; - public const TEST_CONST_PREFIX = self::TEST_CLAZZ . 'TEST_CONST'; + public const string TEST_CONST_PREFIX = self::TEST_CLAZZ . 'TEST_CONST'; - public const TEST_CONST_ARRAY = ['one', 'two']; + public const array TEST_CONST_ARRAY = ['one', 'two']; - public const TEST_CONST_PUBLIC = 'public'; + public const string TEST_CONST_PUBLIC = 'public'; - protected const TEST_CONST_PROTECTED = 'protected'; + protected const string TEST_CONST_PROTECTED = 'protected'; - private const TEST_CONST_PRIVATE = 'private'; + private const string TEST_CONST_PRIVATE = 'private'; } diff --git a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseTest.php b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseTest.php index 9380838..98e402d 100644 --- a/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseTest.php +++ b/tests/PHPUnit/Framework/ConstantCheckTestCaseTest/ConstantCheckTestCaseTest.php @@ -17,6 +17,7 @@ use ollily\Tools\Reflection\UnavailableFieldsTrait; use ollily\Tools\Reflection\UnavailableMethodsTrait; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\ConstantCheckTestCase; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -31,30 +32,32 @@ class ConstantCheckTestCaseTest extends TestCase use UnavailableMethodsTrait; use UnavailableFieldsTrait; - private const TEST_CONST_PREFIX_NAME = 'TEST_CONST_PREFIX'; + private const string TEST_CONST_PREFIX_NAME = 'TEST_CONST_PREFIX'; - private const TEST_CONST_ARRAY_NAME = 'TEST_CONST_ARRAY'; + private const string TEST_CONST_ARRAY_NAME = 'TEST_CONST_ARRAY'; - private const TEST_CONST_ARRAY_SIZE = 2; + private const int TEST_CONST_ARRAY_SIZE = 2; - private const WRONG_CONST = 'WRONG_CONST'; + private const string WRONG_CONST = 'WRONG_CONST'; - private const WRONG_CONST_SIZE = 1; + private const int WRONG_CONST_SIZE = 1; - /** @var ConstantCheckTestCaseClazz */ - protected $o2t; + protected ConstantCheckTestCaseClazz $o2t; - /** @var LoggerInterface */ - private static $logger; + private static LoggerInterface $logger; + #[\Override] public static function setUpBeforeClass(): void { + self::$logger = EasyGoingLogger::init(ConstantCheckTestCaseTest::class); + self::$logger->debug('START'); + parent::setUpBeforeClass(); - // function must be called manually - $sO2t = self::prepareO2t(); - $sO2t::setUpBeforeClass(); + + self::$logger->debug('END'); } + #[\Override] public static function tearDownAfterClass(): void { parent::tearDownAfterClass(); @@ -63,41 +66,31 @@ public static function tearDownAfterClass(): void $sO2t::tearDownAfterClass(); } - /** - * @return ConstantCheckTestCaseClazz - */ - protected static function prepareO2t() + protected static function prepareO2t(): ConstantCheckTestCaseClazz { - return new ConstantCheckTestCaseClazz(); + return new ConstantCheckTestCaseClazz(ConstantCheckTestCaseClazz::class); } /** - * @return array + * @return array */ protected static function prepareAllConsts(): array { return ConstantCheckTestCaseClazz::prepareAllConsts(); } - /** - * @param mixed $name - * @param array $data - * @param string $dataName - */ - public function __construct($name = null, $data = [], $dataName = '') - { - self::$logger = EasyGoingLogger::init(ConstantCheckTestCaseTest::class); - self::$logger->debug('START'); - parent::__construct($name, $data, $dataName); - self::$logger->debug('END'); - } - + #[\Override] public function setUp(): void { self::$logger->debug('START'); + parent::setUp(); + $this->o2t = self::prepareO2t(); + // Must be called manually + $this->o2t::setUpBeforeClass(); $this->o2t->setUp(); + self::$logger->debug('END'); } @@ -161,13 +154,9 @@ public function testIsWithConstCrossCheck(): void } /** - * @param bool $success - * @param bool $crossCheckActive - * @param string $clazz - * @param array $actualConstants - * - * @dataProvider providerCrossCheck + * @param array $actualConstants */ + #[DataProvider('providerCrossCheck')] public function testCrossCheckConstants(bool $success, bool $crossCheckActive, string $clazz, array $actualConstants): void { self::$logger->debug('parameters', [$success, $crossCheckActive, $clazz, $actualConstants]); @@ -193,14 +182,15 @@ public function testCrossCheckConstants(bool $success, bool $crossCheckActive, s public function testUpdateActualConsts(): void { + $this->o2t::setUpBeforeClass(true); + $checkedConsts = TestData::ARRAY_ALPHA5; - /** @var array */ $before = $this->getFieldByReflection(ConstantCheckTestCase::class, 'actualConsts', $this->o2t); + $expected = count($before) + count($checkedConsts); $this->o2t::publicUpdateActualConsts($checkedConsts); - /** @var array */ $actual = $this->getFieldByReflection(ConstantCheckTestCase::class, 'actualConsts', $this->o2t); self::assertCount($expected, $actual); @@ -221,11 +211,9 @@ public function testCheckConstantsCountDisabled(): void } /** - * @param bool $success - * @param array $constants - * - * @dataProvider providerConstants + * @param array $constants */ + #[DataProvider('providerConstants')] public function testVerifyConstAllExists(bool $success, array $constants): void { self::$logger->debug('parameters', [$success, $constants]); @@ -244,11 +232,9 @@ public function testVerifyConstAllExists(bool $success, array $constants): void } /** - * @param bool $success - * @param array $constants - * - * @dataProvider providerConstantsArray + * @param array $constants */ + #[DataProvider('providerConstantsArray')] public function testVerifyConstArrayAllExists(bool $success, array $constants): void { self::$logger->debug('parameters', [$success, $constants]); @@ -266,13 +252,7 @@ public function testVerifyConstArrayAllExists(bool $success, array $constants): $this->verifyConstantsTestResult($success, $exception, [$success, $constants]); } - /** - * @param bool $success - * @param string $constantName - * @param int $expectedSize - * - * @dataProvider providerConstantName - */ + #[DataProvider('providerConstantName')] public function testVerifyConstArraySize(bool $success, string $constantName, int $expectedSize): void { self::$logger->debug('parameters', [$constantName, $expectedSize]); @@ -295,7 +275,7 @@ public function testVerifyConstArraySize(bool $success, string $constantName, in /** * @return array */ - public function providerConstants() + public static function providerConstants(): array { return [ 'emptyList' => [true, []], @@ -308,7 +288,7 @@ public function providerConstants() /** * @return array */ - public function providerConstantsArray() + public static function providerConstantsArray(): array { return [ 'emptyList' => [true, []], @@ -321,7 +301,7 @@ public function providerConstantsArray() /** * @return array */ - public function providerConstantName() + public static function providerConstantName(): array { return [ 'missingName' => [false, '', self::TEST_CONST_ARRAY_SIZE], @@ -334,7 +314,7 @@ public function providerConstantName() /** * @return array */ - public function providerCrossCheck() + public static function providerCrossCheck(): array { return [ 'emptyListDisabled' => [true, false, ConstantCheckTestCaseDummyClazz::class, []], @@ -343,13 +323,13 @@ public function providerCrossCheck() false, true, ConstantCheckTestCaseDummyClazz::class, - array_merge(self::prepareAllConsts(), [ConstantCheckTestCaseDummyClazz::TEST_CLAZZ . self::WRONG_CONST]) + array_merge(self::prepareAllConsts(), [ConstantCheckTestCaseDummyClazz::TEST_CLAZZ . self::WRONG_CONST]), ], 'existConstOneEnabled' => [ false, true, ConstantCheckTestCaseDummyClazz::class, - [ConstantCheckTestCaseDummyClazz::TEST_CLAZZ . self::TEST_CONST_PREFIX_NAME] + [ConstantCheckTestCaseDummyClazz::TEST_CLAZZ . self::TEST_CONST_PREFIX_NAME], ], 'existConstAllEnabled' => [true, true, ConstantCheckTestCaseDummyClazz::class, self::prepareAllConsts()], ]; @@ -358,9 +338,9 @@ public function providerCrossCheck() // Misc functions /** - * @param bool $success - * @param null|\Exception $exception - * @param array $extraData + * @param bool $success + * @param ?\Exception $exception + * @param array $extraData */ protected function verifyConstantsTestResult(bool $success, ?\Exception $exception, array $extraData): void { diff --git a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseClazz.php b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseClazz.php index d6a6141..a5d0496 100644 --- a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseClazz.php +++ b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseClazz.php @@ -24,66 +24,43 @@ */ class EasyGoingTestCaseClazz extends EasyGoingTestCase // NOSONAR: php:S3360 { - public const C_TEST = 1; + public const int C_TEST = 1; + #[\Override] public static function tearDownAfterClass(): void { // Deactivate the check, will be called manually in testcase } - /** - * @return EasyGoingTestCaseDummyClazz - */ - protected static function prepareO2t() + #[\Override] + protected static function prepareO2t(): EasyGoingTestCaseDummyClazz { return new EasyGoingTestCaseDummyClazz(); } - /** - * @return EasyGoingTestCaseDummyClazz - */ - protected function getCasto2t() + #[\Override] + protected function getCasto2t(): EasyGoingTestCaseDummyClazz { return $this->o2t; } // Override the visibility for the test cases - /** - * @param mixed $clazz - * @param string $constantName - * - * @return bool - */ - public static function publicIsConstExist($clazz, string $constantName): bool + public static function publicIsConstExist(mixed $clazz, string $constantName): bool { return parent::isConstExist($clazz, $constantName); } - /** - * @param mixed $clazz - * @param string $constantName - * - * @return mixed - */ - public static function publicGetConstValue($clazz, string $constantName) + public static function publicGetConstValue(mixed $clazz, string $constantName): mixed { return parent::getConstValue($clazz, $constantName); } - /** - * @param mixed $var - * - * @return bool - */ - public static function publicIsPrimitive($var): bool + public static function publicIsPrimitive(mixed $var): bool { return parent::isPrimitive($var); } - /** - * @return string - */ public static function publicGet_called_clazz(): string { return parent::get_called_clazz(); @@ -99,26 +76,17 @@ public static function publicGetAllDefinedConsts(string $clazz): array return parent::getAllDefinedConsts($clazz); } - /** - * @param string $constantName - */ public function publicVerifyConstExists(string $constantName): void { parent::verifyConstExists($constantName); } - /** - * @return string - */ public function publicGet_called_function(): string { return parent::get_called_function(); } - /** - * @return EasyGoingTestCaseDummyClazz - */ - public function publicGetCastO2t() + public function publicGetCastO2t(): EasyGoingTestCaseDummyClazz { return $this->getCasto2t(); } diff --git a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseDummyClazz.php b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseDummyClazz.php index ead8886..2d502a6 100644 --- a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseDummyClazz.php +++ b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseDummyClazz.php @@ -20,15 +20,15 @@ */ class EasyGoingTestCaseDummyClazz { - public const TEST_CLAZZ = self::class . '::'; + public const string TEST_CLAZZ = self::class . '::'; - public const TEST_CONST_PREFIX = self::TEST_CLAZZ . 'TEST_CONST'; + public const string TEST_CONST_PREFIX = self::TEST_CLAZZ . 'TEST_CONST'; - public const TEST_CONST_ARRAY = ['one', 'two']; + public const array TEST_CONST_ARRAY = ['one', 'two']; - public const TEST_CONST_PUBLIC = 'public'; + public const string TEST_CONST_PUBLIC = 'public'; - protected const TEST_CONST_PROTECTED = 'protected'; + protected const string TEST_CONST_PROTECTED = 'protected'; - private const TEST_CONST_PRIVATE = 'private'; + private const string TEST_CONST_PRIVATE = 'private'; } diff --git a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseTest.php b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseTest.php index cf41a8f..aafd03a 100644 --- a/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseTest.php +++ b/tests/PHPUnit/Framework/EasyGoingTestCaseTest/EasyGoingTestCaseTest.php @@ -16,6 +16,7 @@ use Monolog\EasyGoingLogger; use ollily\Tools\Reflection\UnavailableFieldsTrait; use ollily\Tools\Reflection\UnavailableMethodsTrait; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -29,47 +30,46 @@ class EasyGoingTestCaseTest extends TestCase use UnavailableMethodsTrait; use UnavailableFieldsTrait; - /** @var EasyGoingTestCaseClazz */ - protected $o2t; + protected EasyGoingTestCaseClazz $o2t; - /** @var LoggerInterface */ - private static $logger; + private static LoggerInterface $logger; - /** - * @return EasyGoingTestCaseClazz - */ - protected static function prepareO2t() + #[\Override] + public static function setUpBeforeClass(): void { - return new EasyGoingTestCaseClazz(); + self::$logger = EasyGoingLogger::init(EasyGoingTestCaseTest::class); + self::$logger->debug('START'); + + parent::setUpBeforeClass(); + + self::$logger->debug('END'); } - /** - * @return array - */ - protected static function prepareAllConsts(): array + protected static function prepareO2t(): EasyGoingTestCaseClazz { - return EasyGoingTestCaseClazz::prepareAllConsts(); + return new EasyGoingTestCaseClazz(EasyGoingTestCaseClazz::class); } /** - * @param mixed $name - * @param array $data - * @param string $dataName + * @return array */ - public function __construct($name = null, $data = [], $dataName = '') + protected static function prepareAllConsts(): array { - self::$logger = EasyGoingLogger::init(EasyGoingTestCaseTest::class); - self::$logger->debug('START'); - parent::__construct($name, $data, $dataName); - self::$logger->debug('END'); + return EasyGoingTestCaseClazz::prepareAllConsts(); } + #[\Override] public function setUp(): void { self::$logger->debug('START'); + parent::setUp(); + $this->o2t = self::prepareO2t(); + // Must be called manually + $this->o2t::setUpBeforeClass(); $this->o2t->setUp(); + self::$logger->debug('END'); } @@ -110,15 +110,10 @@ public function testTestInit(): void } } - /** - * @param bool $expectedBool - * @param string $constName - * - * @dataProvider providerConstant - */ - public function testIsConstExist(bool $expectedBool, string $constName): void + #[DataProvider('providerConstant')] + public function testIsConstExist(bool $expectedBool, string $constName, string $expected): void { - self::$logger->debug('parameters', [$expectedBool, $constName]); + self::$logger->debug('parameters', [$expectedBool, $constName, $expected]); $actual = $this->o2t::publicIsConstExist($this->o2t->publicGetCastO2t(), $constName); @@ -127,16 +122,10 @@ public function testIsConstExist(bool $expectedBool, string $constName): void self::assertEquals($expectedBool, $actual, sprintf("Not equals: '%s'='%s'", "$expectedBool", "$actual")); } - /** - * @param bool $expectedBool - * @param string $constName - * @param string $expected - * - * @dataProvider providerConstant - */ + #[DataProvider('providerConstant')] public function testGetConstValue(bool $expectedBool, string $constName, string $expected): void { - self::$logger->debug('parameters', [$expectedBool, $constName]); + self::$logger->debug('parameters', [$expectedBool, $constName, $expected]); $actual = $this->o2t::publicGetConstValue($this->o2t->publicGetCastO2t(), $constName); @@ -199,13 +188,13 @@ public function testGet_called_function(): void /** * @return array */ - public function providerConstant() + public static function providerConstant(): array { return [ 'public' => [true, EasyGoingTestCaseDummyClazz::TEST_CONST_PREFIX . '_PUBLIC', 'public'], 'protected' => [true, EasyGoingTestCaseDummyClazz::TEST_CONST_PREFIX . '_PROTECTED', 'protected'], 'private' => [true, EasyGoingTestCaseDummyClazz::TEST_CONST_PREFIX . '_PRIVATE', 'private'], - 'notexist' => [false, EasyGoingTestCaseDummyClazz::TEST_CONST_PREFIX . '_NOTEXISTS', ''] + 'notexist' => [false, EasyGoingTestCaseDummyClazz::TEST_CONST_PREFIX . '_NOTEXISTS', ''], ]; } } diff --git a/tests/Tools/Batch/BatchTaskHelperTest.php b/tests/Tools/Batch/BatchTaskHelperTest.php index 8bab1f8..bd35a91 100644 --- a/tests/Tools/Batch/BatchTaskHelperTest.php +++ b/tests/Tools/Batch/BatchTaskHelperTest.php @@ -13,20 +13,14 @@ namespace ollily\Tools\Batch; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; class BatchTaskHelperTest extends TestCase { - /** - * @param string $expectedKey - * @param int $expectedCount - * @param bool $expectedEmpty - * @param null|string $listKey - * - * @dataProvider providerTaskList - */ - public function testGetTaskList(string $expectedKey, int $expectedCount, bool $expectedEmpty, ?string $listKey = null): void + #[DataProvider('providerTaskList')] + public function testGetTaskList(string $expectedKey, int $expectedCount, bool $expectedEmpty, string $listKey): void { $actual = BatchTaskHelper::getTaskList($listKey); @@ -36,16 +30,8 @@ public function testGetTaskList(string $expectedKey, int $expectedCount, bool $e self::assertEquals($expectedEmpty, $actual->isEmpty()); } - /** - * @param string $expectedKey - * @param int $expectedCount - * @param bool $expectedEmpty - * @param string $fileName - * @param null|string $listKey - * - * @dataProvider providerTaskListFile - */ - public function testReadTaskList(string $expectedKey, int $expectedCount, bool $expectedEmpty, string $fileName, ?string $listKey = null): void + #[DataProvider('providerTaskListFile')] + public function testReadTaskList(string $expectedKey, int $expectedCount, bool $expectedEmpty, string $fileName, string $listKey): void { $actual = BatchTaskHelper::readTaskList($fileName, $listKey); @@ -60,20 +46,20 @@ public function testReadTaskList(string $expectedKey, int $expectedCount, bool $ /** * @return array */ - public function providerTaskList(): array + public static function providerTaskList(): array { return [ - 'empty' => [BatchTaskHelper::DEFAULT, 0, true], + 'empty' => [BatchTaskHelper::DEFAULT, 0, true, TestData::KEY_EMPTY], ]; } /** * @return array */ - public function providerTaskListFile(): array + public static function providerTaskListFile(): array { return [ - 'empty' => [BatchTaskHelper::DEFAULT, 0, true, TestData::FILE_FILENAME_EMPTY], + 'empty' => [BatchTaskHelper::DEFAULT, 0, true, TestData::FILE_FILENAME_EMPTY, TestData::KEY_EMPTY], ]; } } diff --git a/tests/Tools/Batch/TaskItemTest.php b/tests/Tools/Batch/TaskItemTest.php index ab97639..2c2c917 100644 --- a/tests/Tools/Batch/TaskItemTest.php +++ b/tests/Tools/Batch/TaskItemTest.php @@ -13,27 +13,23 @@ namespace ollily\Tools\Batch; -use PHPUnit\Framework\EasyGoingTestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\EasyGoingTestCase; class TaskItemTest extends EasyGoingTestCase { - public const KEY = TestData::KEY_NUM1; + public const int KEY = TestData::KEY_NUM1; - public const DATA = [TestData::DATA_ALPHA1, TestData::DATA_BOOL_T]; + public const array DATA = [TestData::DATA_ALPHA1, TestData::DATA_BOOL_T]; - /** - * @return ITaskItem - */ - protected static function prepareO2t() + #[\Override] + protected static function prepareO2t(): ITaskItem { return new TaskItem(self::KEY, self::DATA); } - /** - * @return ITaskItem - */ - protected function getCasto2t() + #[\Override] + protected function getCasto2t(): ITaskItem { return $this->o2t; } diff --git a/tests/Tools/Batch/TaskListTest.php b/tests/Tools/Batch/TaskListTest.php index 444bdb5..f11a64e 100644 --- a/tests/Tools/Batch/TaskListTest.php +++ b/tests/Tools/Batch/TaskListTest.php @@ -14,59 +14,49 @@ namespace ollily\Tools\Batch; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\EasyGoingTestCase; class TaskListTest extends EasyGoingTestCase { - /** @var string */ - public const KEY = TestData::KEY_ALPHA1; + public const string KEY = TestData::KEY_ALPHA1; - /** @var string */ - public const DATA = TestData::DATA_ALPHA1; + public const string DATA = TestData::DATA_ALPHA1; - /** @var string */ - protected static $emptyFileName = TestData::FILE_FILENAME_EMPTY; - - /** @var string */ - protected static $existingFile; - - /** @var string */ - protected static $emptyFile; - - /** @var string */ - private $writeTaskListFile = ''; + private string $writeTaskListFile = ''; + #[\Override] protected function tearDown(): void { TestData::cleanupTempFile($this->writeTaskListFile); } /** - * @param mixed $name - * @param array $data - * @param string $dataName + * @return array */ - public function __construct($name = null, $data = [], $dataName = '') + public static function prepareFiles(): array { - parent::__construct($name, $data, $dataName); $reflector = new \ReflectionClass(self::class); - $path = '' . realpath('' . $reflector->getFileName()); - self::$existingFile = str_replace(TestData::FILE_EXT_PHP, TestData::FILE_EXT_CSV, $path); - self::$emptyFile = str_replace(TestData::FILE_EXT_PHP, 'Empty' . TestData::FILE_EXT_CSV, $path); + $path = realpath('' . $reflector->getFileName()); + if ($path !== false) { + $emptyFile = str_replace(TestData::FILE_EXT_PHP, 'Empty' . TestData::FILE_EXT_CSV, $path); + $existingFile = str_replace(TestData::FILE_EXT_PHP, TestData::FILE_EXT_CSV, $path); + } else { + $emptyFile = ''; + $existingFile = ''; + } + + return [TestData::FILE_FILENAME_EMPTY,$emptyFile, $existingFile]; } - /** - * @return TaskList - */ - protected static function prepareO2t() + #[\Override] + protected static function prepareO2t(): TaskList { return new TaskList(self::KEY); } - /** - * @return TaskList - */ - protected function getCasto2t() + #[\Override] + protected function getCasto2t(): TaskList { return $this->o2t; } @@ -128,12 +118,7 @@ public function testNextTask(): void self::assertNull($item); } - /** - * @param bool $expected - * @param string $fileName - * - * @dataProvider providerTaskListFile - */ + #[DataProvider('providerTaskListFile')] public function testReadFileFile(bool $expected, int $expectedCount, string $fileName): void { $this->o2t = new TaskList(self::class); @@ -164,12 +149,12 @@ public function testStoreFile(): void /** * @return array */ - public function providerTaskListFile(): array + public static function providerTaskListFile(): array { return [ - 'emptyFileName' => [false, 0,self::$emptyFileName], - 'emptyFile' => [true, 0,self::$emptyFile], - 'existingFile' => [true, 3, self::$existingFile], + 'emptyFileName' => [false, 0, self::prepareFiles()[0]], + 'emptyFile' => [true, 0, self::prepareFiles()[1]], + 'existingFile' => [true, 3, self::prepareFiles()[2]], ]; } @@ -181,7 +166,7 @@ public function providerTaskListFile(): array * * @return array */ - protected function prepareTaskItem($taskListKey, int $count): array + protected function prepareTaskItem(mixed $taskListKey, int $count): array { $items = []; diff --git a/tests/Tools/EmergencyTest.php b/tests/Tools/EmergencyTest.php index ae11da3..2678da3 100644 --- a/tests/Tools/EmergencyTest.php +++ b/tests/Tools/EmergencyTest.php @@ -13,33 +13,28 @@ namespace ollily\Tools; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class EmergencyTest extends TestCase { - public const CODE_NEG1 = -1; + public const int CODE_NEG1 = -1; - public const CODE_0 = 0; + public const int CODE_0 = 0; - public const CODE_89 = 89; + public const int CODE_89 = 89; - public const CODE_123 = 123; + public const int CODE_123 = 123; - public const CODE_255 = 255; + public const int CODE_255 = 255; - public const MSG_EMPTY = ''; + public const string MSG_EMPTY = ''; - public const MSG_01 = 'ERR-MSG'; + public const string MSG_01 = 'ERR-MSG'; - public const MSG_02 = 'There is something worried'; + public const string MSG_02 = 'There is something worried'; - /** - * @param int $errCode - * @param string $errMessage - * @param int $expected - * - * @dataProvider providerBreakSystem - */ + #[DataProvider('providerBreakSystem')] public function testBreakSystem(int $errCode, string $errMessage, int $expected): void { $actual = Emergency::breakSystem($errCode, $errMessage, true); @@ -47,12 +42,7 @@ public function testBreakSystem(int $errCode, string $errMessage, int $expected) self::assertEquals($expected, $actual); } - /** - * @param \Throwable $throwable - * @param int $expected - * - * @dataProvider providerExceptionStop - */ + #[DataProvider('providerExceptionStop')] public function testExceptionStop(\Throwable $throwable, int $expected): void { $actual = Emergency::exceptionStop($throwable, true); @@ -65,7 +55,7 @@ public function testExceptionStop(\Throwable $throwable, int $expected): void /** * @return array */ - public function providerBreakSystem(): array + public static function providerBreakSystem(): array { return [ 'Default' => [self::CODE_0, self::MSG_EMPTY, Emergency::ERR_CODE_DEFAULT], @@ -78,7 +68,7 @@ public function providerBreakSystem(): array /** * @return array */ - public function providerExceptionStop(): array + public static function providerExceptionStop(): array { return [ 'Default' => [new \Exception(), Emergency::ERR_CODE_DEFAULT], diff --git a/tests/Tools/EnvironmentVariableTraitTest.php b/tests/Tools/EnvironmentVariableTraitTest.php index 81a4205..c04f2f8 100644 --- a/tests/Tools/EnvironmentVariableTraitTest.php +++ b/tests/Tools/EnvironmentVariableTraitTest.php @@ -13,26 +13,26 @@ namespace ollily\Tools; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\TestCase; class EnvironmentVariableTraitTest extends TestCase { use EnvironmentVariableTrait; - private const PROJECT_NAME = 'ezkoding'; + private const string PROJECT_NAME = 'ezkoding'; - private const HOME_WIN = 'USERPROFILE'; + private const string HOME_WIN = 'USERPROFILE'; - private const HOME_LINUX = 'HOME'; + private const string HOME_LINUX = 'HOME'; - private const HOME_NOTEXIST = 'NOTEXISTS'; + private const string HOME_NOTEXIST = 'NOTEXISTS'; public function testHomeDefault(): void { $actual = self::getHome(); - $this->validateActualContains($actual, DIRECTORY_SEPARATOR); + $this->validateActualContains(DIRECTORY_SEPARATOR, $actual); } public function testHomeUserProfileDirect(): void @@ -42,7 +42,7 @@ public function testHomeUserProfileDirect(): void $actual = self::getHome(self::HOME_LINUX); } - $this->validateActualContains($actual, DIRECTORY_SEPARATOR); + $this->validateActualContains(DIRECTORY_SEPARATOR, $actual); } public function testHomeUserProfileIndirect(): void @@ -56,30 +56,34 @@ public function testGetProjectRoot(): void { $actual = self::getProjectRoot(); - $this->validateActualEnds($actual, self::PROJECT_NAME); + $this->validateActualEnds(self::PROJECT_NAME, $actual); } public function testGetComposerFilePath(): void { $actual = self::getComposerFilePath(); - $this->validateActualEnds($actual, self::PROJECT_NAME); + $this->validateActualEnds(self::PROJECT_NAME, $actual); } public function testGetProjectRootFallback(): void { $actual = self::getProjectRootFallback(); - $this->validateActualEnds($actual, self::PROJECT_NAME); + $this->validateActualEnds(self::PROJECT_NAME, $actual); } - private function validateActualContains(string $actual, string $expected): void + private function validateActualContains(string $expected, string $actual): void { self::assertNotEmpty($actual); self::assertStringContainsString($expected, $actual); } - private function validateActualEnds(string $actual, string $expected): void + /** + * @param non-empty-string $expected + * @param string $actual + */ + private function validateActualEnds(string $expected, string $actual): void { self::assertNotEmpty($actual); self::assertStringEndsWith($expected, $actual); diff --git a/tests/Tools/PhpVersionTraitTest.php b/tests/Tools/PhpVersionTraitTest.php index c5151fa..8542f20 100644 --- a/tests/Tools/PhpVersionTraitTest.php +++ b/tests/Tools/PhpVersionTraitTest.php @@ -13,25 +13,21 @@ namespace ollily\Tools; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; class PhpVersionTraitTest extends TestCase { use PhpVersionTrait; - public const PHP_VERSION_MIN = '0.0.1'; + public const string PHP_VERSION_MIN = '0.0.1'; - public const PHP_VERSION_CURR = PHP_VERSION; + public const string PHP_VERSION_CURR = PHP_VERSION; - public const PHP_VERSION_MAX = '99.99.999'; + public const string PHP_VERSION_MAX = '99.99.999'; - /** - * @param bool $expected - * @param string $checkVersion - * - * @dataProvider providerPhpVersion - */ + #[DataProvider('providerPhpVersion')] public function testIsPhpGreater(bool $expected, string $checkVersion): void { $actual = $this->isPhpGreater($checkVersion); @@ -43,7 +39,7 @@ public function testIsPhpGreater(bool $expected, string $checkVersion): void /** * @return array */ - public function providerPhpVersion(): array + public static function providerPhpVersion(): array { return [ 'equal' => [true, self::PHP_VERSION_CURR], diff --git a/tests/Tools/Reflection/ChildClazzesHelperTest.php b/tests/Tools/Reflection/ChildClazzesHelperTest.php index 011fc48..b09b6e9 100644 --- a/tests/Tools/Reflection/ChildClazzesHelperTest.php +++ b/tests/Tools/Reflection/ChildClazzesHelperTest.php @@ -13,19 +13,16 @@ namespace ollily\Tools\Reflection; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; class ChildClazzesHelperTest extends TestCase { /** - * @param int $expected - * @param string $clazzName * @param array $childClazzes - * @param bool $isEqual - * - * @dataProvider providerChildClazzes */ + #[DataProvider('providerChildClazzes')] public function testAllChildren(int $expected, string $clazzName, array $childClazzes, bool $isEqual = true): void { $actual = ChildClazzesHelper::getAllChildren($clazzName); @@ -45,7 +42,7 @@ public function testAllChildren(int $expected, string $clazzName, array $childCl /** * @return array */ - public function providerChildClazzes(): array + public static function providerChildClazzes(): array { return [ 'noChildren' => [0, ChildClazzesHelperTest::class, []], diff --git a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitDummyClazz.php b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitDummyClazz.php index 1f344f6..84445a5 100644 --- a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitDummyClazz.php +++ b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitDummyClazz.php @@ -23,12 +23,9 @@ */ class UnavailableFieldsTraitDummyClazz { - /** @var string */ - public $publicField = 'publicFieldValue'; + public string $publicField = 'publicFieldValue'; - /** @var string */ - protected $protectedField = 'protectedFieldValue'; + protected string $protectedField = 'protectedFieldValue'; - /** @var string */ - private $privateField = 'privateFieldValue'; + private string $privateField = 'privateFieldValue'; } diff --git a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTest.php b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTest.php index c2924c1..5a413f3 100644 --- a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTest.php +++ b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTest.php @@ -23,12 +23,12 @@ */ class UnavailableFieldsTraitTest extends TestCase { - /** @var UnavailableFieldsTraitTestO2tClazz */ - protected $o2t; + protected UnavailableFieldsTraitTestO2tClazz $o2t; - /** @var string[] */ - private $fieldNames = ['publicField', 'protectedField', 'privateField']; + /** @var array */ + private array $fieldNames = ['publicField', 'protectedField', 'privateField']; + #[\Override] public function setUp(): void { parent::setUp(); @@ -62,7 +62,6 @@ public function testGetFieldFromO2t(): void public function testGetFieldFromO2tReturnNull(): void { - /** @var UnavailableFieldsTraitTestWrongO2tClazz $o2tb */ $o2tb = new UnavailableFieldsTraitTestWrongO2tClazz(); foreach ($this->fieldNames as $fieldName) { self::assertNull($o2tb->publicGetFieldFromO2t($fieldName)); diff --git a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestO2tClazz.php b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestO2tClazz.php index 7b06c68..30f26ec 100644 --- a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestO2tClazz.php +++ b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestO2tClazz.php @@ -25,39 +25,24 @@ class UnavailableFieldsTraitTestO2tClazz { use UnavailableFieldsTrait; - /** @var mixed */ - protected $o2t; + protected UnavailableFieldsTraitDummyClazz $o2t; public function __construct() { $this->o2t = new UnavailableFieldsTraitDummyClazz(); } - /** - * @param string $fieldName - * - * @return null|mixed - */ - public function publicGetFieldFromO2t(string $fieldName) + public function publicGetFieldFromO2t(string $fieldName): mixed { return $this->getFieldFromO2t($fieldName); } - /** - * @param string $fieldName - * - * @return null|mixed - */ - public function publicGetFieldByReflection(string $fieldName) + public function publicGetFieldByReflection(string $fieldName): mixed { return $this->getFieldByReflection(UnavailableFieldsTraitDummyClazz::class, $fieldName, $this->o2t); } - /** - * @param string $fieldName - * @param null|mixed $newValue - */ - public function publicSetFieldByReflection(string $fieldName, $newValue): void + public function publicSetFieldByReflection(string $fieldName, mixed $newValue): void { $this->setFieldByReflection(UnavailableFieldsTraitDummyClazz::class, $fieldName, $this->o2t, $newValue); } diff --git a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestWrongO2tClazz.php b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestWrongO2tClazz.php index 1e54356..2ada326 100644 --- a/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestWrongO2tClazz.php +++ b/tests/Tools/Reflection/UnavailableFieldsTraitTest/UnavailableFieldsTraitTestWrongO2tClazz.php @@ -25,20 +25,14 @@ class UnavailableFieldsTraitTestWrongO2tClazz { use UnavailableFieldsTrait; - /** @var mixed */ - private $wrongO2t; + private UnavailableFieldsTraitDummyClazz $wrongO2t; public function __construct() { $this->wrongO2t = new UnavailableFieldsTraitDummyClazz(); } - /** - * @param string $fieldName - * - * @return null|mixed - */ - public function publicGetFieldFromO2t(string $fieldName) + public function publicGetFieldFromO2t(string $fieldName): mixed { return $this->getFieldFromO2t($fieldName); } diff --git a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTest.php b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTest.php index d560672..c66a717 100644 --- a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTest.php +++ b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTest.php @@ -23,12 +23,12 @@ */ class UnavailableMethodsTraitTest extends TestCase { - /** @var UnavailableMethodsTraitTestO2tClazz */ - protected $o2t; + protected UnavailableMethodsTraitTestO2tClazz $o2t; - /** @var string[] */ - private $methodNames = ['publicFunc', 'protectedFunc', 'privateFunc']; + /** @var array */ + private array $methodNames = ['publicFunc', 'protectedFunc', 'privateFunc']; + #[\Override] public function setUp(): void { parent::setUp(); @@ -51,7 +51,6 @@ public function testCallMethodOnO2t(): void public function testCallMethodOnO2tReturnNull(): void { - /** @var UnavailableMethodsTraitTestWrongO2tClazz $o2tb */ $o2tb = new UnavailableMethodsTraitTestWrongO2tClazz(); foreach ($this->methodNames as $methodName) { self::assertNull($o2tb->publicCallMethodOnO2t($methodName)); diff --git a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestO2tClazz.php b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestO2tClazz.php index 30802aa..b8668cd 100644 --- a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestO2tClazz.php +++ b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestO2tClazz.php @@ -25,30 +25,19 @@ class UnavailableMethodsTraitTestO2tClazz { use UnavailableMethodsTrait; - /** @var mixed */ - protected $o2t; + protected UnavailableMethodsTraitDummyClazz $o2t; public function __construct() { $this->o2t = new UnavailableMethodsTraitDummyClazz(); } - /** - * @param string $methodName - * - * @return null|mixed - */ - public function publicCallMethodOnO2t(string $methodName) + public function publicCallMethodOnO2t(string $methodName): mixed { return $this->callMethodOnO2t($methodName); } - /** - * @param string $methodName - * - * @return null|mixed - */ - public function publicCallMethodByReflection(string $methodName) + public function publicCallMethodByReflection(string $methodName): mixed { return $this->callMethodByReflection(UnavailableMethodsTraitDummyClazz::class, $methodName, $this->o2t); } diff --git a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestWrongO2tClazz.php b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestWrongO2tClazz.php index fc07917..ad85b68 100644 --- a/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestWrongO2tClazz.php +++ b/tests/Tools/Reflection/UnavailableMethodsTraitTest/UnavailableMethodsTraitTestWrongO2tClazz.php @@ -25,24 +25,15 @@ class UnavailableMethodsTraitTestWrongO2tClazz { use UnavailableMethodsTrait; - /** - * @var mixed - * - * @SuppressWarnings("PHPMD.UnusedPrivateField") - */ - private $wrongO2t; + /** @SuppressWarnings("PHPMD.UnusedPrivateField") */ + private UnavailableMethodsTraitDummyClazz $wrongO2t; public function __construct() { $this->wrongO2t = new UnavailableMethodsTraitDummyClazz(); } - /** - * @param string $methodName - * - * @return null|mixed - */ - public function publicCallMethodOnO2t(string $methodName) + public function publicCallMethodOnO2t(string $methodName): mixed { return $this->callMethodOnO2t($methodName); } diff --git a/tests/Tools/String/ImplodeTraitTest.php b/tests/Tools/String/ImplodeTraitTest.php index d1cae96..4e580d5 100644 --- a/tests/Tools/String/ImplodeTraitTest.php +++ b/tests/Tools/String/ImplodeTraitTest.php @@ -13,8 +13,9 @@ namespace ollily\Tools\String; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; /** * This is the test clazz which will test the test clazz. @@ -23,44 +24,26 @@ */ class ImplodeTraitTest extends TestCase { - public const ITEM_SEP = '#'; + public const string ITEM_SEP = '#'; - public const KEY_SEP = '=>'; + public const string KEY_SEP = '=>'; - /** @var ImplodeTraitTestDummyClazz */ - protected $o2t; + protected ImplodeTraitTestDummyClazz $o2t; + #[\Override] public function setUp(): void { parent::setUp(); $this->o2t = new ImplodeTraitTestDummyClazz(); } - public function testDEFAULT_GLUE(): void - { - $expected = ','; - - $actual = $this->o2t->DEFAULT_GLUE(); - - self::assertEquals($expected, $actual); - } - - public function testDEFAULT_ITEM_SEP(): void - { - $expected = ';'; - - $actual = $this->o2t->DEFAULT_ITEM_SEP(); - - self::assertEquals($expected, $actual); - } - public function testImplode_recursiveDefault(): void { $testData = $this->o2t->traitData; $expectedKeyCount = 0; $expectedItemCount = count($testData) - 1; - $actual = $this->o2t->implode_recursive(self::ITEM_SEP, $testData); + $actual = $this->o2t::implode_recursive(self::ITEM_SEP, $testData); $this->verifyResult($actual, $testData, $expectedKeyCount, $expectedItemCount); } @@ -71,7 +54,7 @@ public function testImplode_recursiveCustom(): void $expectedKeyCount = count($testData); $expectedItemCount = $expectedKeyCount - 1; - $actual = $this->o2t->implode_recursive(self::ITEM_SEP, $testData, true, true); + $actual = $this->o2t::implode_recursive(self::ITEM_SEP, $testData, true, true); $this->verifyResult($actual, $testData, $expectedKeyCount, $expectedItemCount); } @@ -82,7 +65,7 @@ public function testImplode_recursive_ObjectCustom(): void $expectedKeyCount = count($testData) + count($testData[1]); $expectedItemCount = $expectedKeyCount - 2; - $actual = $this->o2t->implode_recursive(self::ITEM_SEP, $testData, true, true); + $actual = $this->o2t::implode_recursive(self::ITEM_SEP, $testData, true, true); $this->verifyResult($actual, $testData, $expectedKeyCount, $expectedItemCount, true); } @@ -91,9 +74,8 @@ public function testImplode_recursive_ObjectCustom(): void * @param int $expectedCount * @param array $testData * @param int $preserveKeys - * - * @dataProvider providerArrayFlatten */ + #[DataProvider('providerArrayFlatten')] public function testArray_flatten(int $expectedCount, array $testData, int $preserveKeys): void { $actual = $this->o2t->array_flatten($testData, $preserveKeys); @@ -101,14 +83,7 @@ public function testArray_flatten(int $expectedCount, array $testData, int $pres self::assertCount($expectedCount, $actual); } - /** - * @param mixed $actual - * @param mixed $testData - * @param int $expectedKeyCount - * @param int $expectedItemCount - * @param bool $withClazz - */ - public function verifyResult($actual, $testData, int $expectedKeyCount, int $expectedItemCount, bool $withClazz = false): void + public function verifyResult(mixed $actual, mixed $testData, int $expectedKeyCount, int $expectedItemCount, bool $withClazz = false): void { self::assertNotEmpty($actual); self::assertEquals($expectedKeyCount, substr_count($actual, self::KEY_SEP)); @@ -117,10 +92,10 @@ public function verifyResult($actual, $testData, int $expectedKeyCount, int $exp if ($withClazz) { if (is_array($expected)) { foreach ($expected as $innerExpected) { - self::assertStringContainsString('' . get_class($innerExpected), $actual); + self::assertStringContainsString('' . $innerExpected::class, $actual); } } else { - self::assertStringContainsString('' . get_class($expected), $actual); + self::assertStringContainsString('' . $expected::class, $actual); } } else { self::assertStringContainsString($expected, $actual); @@ -133,7 +108,7 @@ public function verifyResult($actual, $testData, int $expectedKeyCount, int $exp /** * @return array */ - public function providerArrayFlatten(): array + public static function providerArrayFlatten(): array { return [ 'emptyDefault' => [0, [], 0], @@ -141,14 +116,14 @@ public function providerArrayFlatten(): array 'oneLevelDefault' => [4, [ TestData::DATA_ALPHA1, TestData::ARRAY_ALPHA2, - TestData::DATA_BOOL_F - ], 0 + TestData::DATA_BOOL_F, + ], 0, ], 'twoLevelDefault' => [8, [ TestData::ARRAY_ALPHA2, [TestData::ARRAY_ALPHA2, TestData::ARRAY_ALPHA2], - TestData::ARRAY_ALPHA2 - ], 0 + TestData::ARRAY_ALPHA2, + ], 0, ], ]; } diff --git a/tests/Tools/String/ImplodeTraitTestDummyClazz.php b/tests/Tools/String/ImplodeTraitTestDummyClazz.php index 1963e01..4136732 100644 --- a/tests/Tools/String/ImplodeTraitTestDummyClazz.php +++ b/tests/Tools/String/ImplodeTraitTestDummyClazz.php @@ -25,17 +25,17 @@ class ImplodeTraitTestDummyClazz use ImplodeTrait; /** @var array */ - public $traitData = TestData::ARRAY_ALPHA2; + public array $traitData = TestData::ARRAY_ALPHA2; /** @var array */ - public $traitObject = TestData::ARRAY_EMPTY; + public array $traitObject = TestData::ARRAY_EMPTY; public function __construct() { $this->traitObject[] = new ImplodeTraitTestFooClazz(); $this->traitObject[] = [ TestData::KEY_NUM1 => new ImplodeTraitTestFooClazz(), - TestData::KEY_NUM2 => new ImplodeTraitTestFooClazz() + TestData::KEY_NUM2 => new ImplodeTraitTestFooClazz(), ]; } } diff --git a/tests/Tools/String/ImplodeTraitTestFooClazz.php b/tests/Tools/String/ImplodeTraitTestFooClazz.php index 69a972a..7b16cf2 100644 --- a/tests/Tools/String/ImplodeTraitTestFooClazz.php +++ b/tests/Tools/String/ImplodeTraitTestFooClazz.php @@ -23,8 +23,8 @@ class ImplodeTraitTestFooClazz { /** @var array */ - public $dummyData = TestData::ARRAY_NUM_KEY2; + public array $dummyData = TestData::ARRAY_NUM_KEY2; /** @var array */ - public $dummyEmpty = TestData::ARRAY_EMPTY; + public array $dummyEmpty = TestData::ARRAY_EMPTY; } diff --git a/tests/Tools/String/ToStringTraitTest.php b/tests/Tools/String/ToStringTraitTest.php index e281c08..66a218a 100644 --- a/tests/Tools/String/ToStringTraitTest.php +++ b/tests/Tools/String/ToStringTraitTest.php @@ -13,8 +13,10 @@ namespace ollily\Tools\String; -use PHPUnit\Framework\TestCase; use ollily\Tools\Test\TestData; +use ollily\Tools\Test\TestDataFoo; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; /** * This is the test clazz which will test the test clazz. @@ -25,17 +27,28 @@ class ToStringTraitTest extends TestCase { use ImplodeTrait; - public const FORMAT_ARRAY = '%s:[%s]'; + public const string FORMAT_OBJECT = '{%s Object( [greeting] => %s)}'; + + public const string FORMAT_OBJECT_IN_OBJECT = '{%s Object( [greeting] => %s Object ( [fooValue:%s:private] => %s ))}'; + + public const string FORMAT_ARRAY_IN_OBJECT = '{%s Object( [greeting] => Array ( %s ))}'; + + public const string FORMAT_ARRAY_IN_OBJECT_2 = '{%s Object( [greeting] => Array ( [%s] => %s Object ( [fooValue:%s:private] => %s ) ))}'; + + public const string FORMAT_ARRAY_KEY_NUM_SEARCH = '/(\w*)=>/'; - public const FORMAT_ITEMS = '%s:{%s}'; + public const string FORMAT_ARRAY_KEY_ALPHA_SEARCH = "/'([\w\W]+?)'=>/"; - public const FORMAT_NUM = '%s:%s'; + public const string FORMAT_ARRAY_ELEM_REPLACE = '[$1] => '; - public const FORMAT_ALPHA = '%s:\'%s\''; + public const string FORMAT_ARRAY_SPACE_SEP = ' '; - /** @var ToStringTraitTestDummyClazz */ - protected $o2t; + /** @var array */ + public const array ILLEGAL_CHARS = [" \n", "\n ", "\n", " "]; + protected ToStringTraitTestDummyClazz $o2t; + + #[\Override] protected function setUp(): void { parent::setUp(); @@ -55,19 +68,14 @@ public function testWakeup(): void $this->o2t->__wakeup(); } - /** - * @param mixed $data - * @param string $expected - * - * @dataProvider provideDataToString - */ - public function testToString($data, string $expected): void + #[DataProvider('provideDataToString')] + public function testToString(mixed $data, string $expected): void { $actualObj = new ToStringTraitTestDummyClazz($data); $actual = $actualObj->__toString(); - self::assertEquals($expected, $actual); + self::assertEquals($expected, str_replace(self::ILLEGAL_CHARS, '', $actual)); } /** @@ -75,37 +83,66 @@ public function testToString($data, string $expected): void * * @psalm-suppress InvalidArgument */ - public function provideDataToString(): array + public static function provideDataToString(): array { return [ 'StringAsValue' => [ TestData::DATA_ALPHA1, - sprintf(self::FORMAT_ALPHA, ToStringTraitTestDummyClazz::class, TestData::DATA_ALPHA1) + sprintf(self::FORMAT_OBJECT, ToStringTraitTestDummyClazz::class, TestData::DATA_ALPHA1), ], 'IntegerAsValue' => [ TestData::DATA_NUM1, - sprintf(self::FORMAT_NUM, ToStringTraitTestDummyClazz::class, TestData::DATA_NUM1), + sprintf(self::FORMAT_OBJECT, ToStringTraitTestDummyClazz::class, TestData::DATA_NUM1), ], 'BoolAsValue' => [ TestData::DATA_BOOL_T, - sprintf(self::FORMAT_NUM, ToStringTraitTestDummyClazz::class, TestData::DATA_BOOL_T), + sprintf(self::FORMAT_OBJECT, ToStringTraitTestDummyClazz::class, TestData::DATA_BOOL_T), ], 'ObjectAsValue' => [ TestData::DATA_OBJECT1(), - sprintf(self::FORMAT_NUM, ToStringTraitTestDummyClazz::class, TestData::DATA_OBJECT1()), + sprintf( + self::FORMAT_OBJECT_IN_OBJECT, + ToStringTraitTestDummyClazz::class, + TestDataFoo::class, + TestDataFoo::class, + TestData::DATA_NUM1 + ), ], 'ArrayWithNumKey' => [ TestData::ARRAY_ALPHA3, - sprintf(self::FORMAT_ARRAY, ToStringTraitTestDummyClazz::class, $this->implode_recursive(TestData::ARRAY_ITEM_SEP, TestData::ARRAY_ALPHA3)), + sprintf( + self::FORMAT_ARRAY_IN_OBJECT, + ToStringTraitTestDummyClazz::class, + preg_replace( + self::FORMAT_ARRAY_KEY_NUM_SEARCH, + self::FORMAT_ARRAY_ELEM_REPLACE, + self::implode_recursive(self::FORMAT_ARRAY_SPACE_SEP, TestData::ARRAY_ALPHA3, false, true) + ) + ), ], 'ArrayWithAlphaKeys' => [ TestData::ARRAY_ALPHA_KEY2, - sprintf(self::FORMAT_ARRAY, ToStringTraitTestDummyClazz::class, $this->implode_recursive(TestData::ARRAY_ITEM_SEP, TestData::ARRAY_ALPHA_KEY2)), + sprintf( + self::FORMAT_ARRAY_IN_OBJECT, + ToStringTraitTestDummyClazz::class, + preg_replace( + self::FORMAT_ARRAY_KEY_ALPHA_SEARCH, + self::FORMAT_ARRAY_ELEM_REPLACE, + self::implode_recursive(self::FORMAT_ARRAY_SPACE_SEP, TestData::ARRAY_ALPHA_KEY2, false, true) + ) + ), ], 'ArrayWithObjectValues' => [ - TestData::ARRAY_OBJECT2(), - sprintf(self::FORMAT_ARRAY, ToStringTraitTestDummyClazz::class, $this->implode_recursive(TestData::ARRAY_ITEM_SEP, TestData::ARRAY_OBJECT2())), - ] + TestData::ARRAY_OBJECT1(), + sprintf( + self::FORMAT_ARRAY_IN_OBJECT_2, + ToStringTraitTestDummyClazz::class, + TestData::KEY_ALPHA1, + TestDataFoo::class, + TestDataFoo::class, + TestData::DATA_NUM1 + ), + ], ]; } } diff --git a/tests/Tools/String/ToStringTraitTestDummyClazz.php b/tests/Tools/String/ToStringTraitTestDummyClazz.php index b2bc66d..84a1b43 100644 --- a/tests/Tools/String/ToStringTraitTestDummyClazz.php +++ b/tests/Tools/String/ToStringTraitTestDummyClazz.php @@ -18,18 +18,13 @@ * * @see ToStringTraitTest */ - class ToStringTraitTestDummyClazz { use ToStringTrait; - /** @var mixed */ - public $greeting = 'hello'; + public mixed $greeting = 'hello'; - /** - * @param mixed $greeting - */ - public function __construct($greeting = null) + public function __construct(mixed $greeting = null) { if (!is_null($greeting)) { $this->greeting = $greeting; @@ -37,12 +32,11 @@ public function __construct($greeting = null) } /** - * @return mixed - * * @SuppressWarnings("PHPMD.CamelCaseMethodName") */ - protected function __toStringValues() + #[\Override] + protected function __toStringValues(): mixed { - return $this->greeting; + return $this; } } diff --git a/tests/Tools/Test/TestDataFooTest.php b/tests/Tools/Test/TestDataFooTest.php index 6a20d64..303c156 100644 --- a/tests/Tools/Test/TestDataFooTest.php +++ b/tests/Tools/Test/TestDataFooTest.php @@ -17,13 +17,13 @@ class TestDataFooTest extends TestCase { - /** @var TestDataFoo */ - private $o2t; + private TestDataFoo $o2t; + #[\Override] protected function setUp(): void { parent::setUp(); - $this->o2t = TestDataFoo::init(); + $this->o2t = TestDataFoo::init(TestData::KEY_ALPHA1); } public function testInit(): void @@ -33,8 +33,10 @@ public function testInit(): void public function testToString(): void { + $expected = sprintf('%s:\'%s\'', get_class($this->o2t), TestData::KEY_ALPHA1); + $actual = $this->o2t->__toString(); - self::assertEmpty($actual); + self::assertEquals($expected, $actual); } } diff --git a/tests/Tools/Test/TestDataTest.php b/tests/Tools/Test/TestDataTest.php index e1bcddb..5183cc4 100644 --- a/tests/Tools/Test/TestDataTest.php +++ b/tests/Tools/Test/TestDataTest.php @@ -13,6 +13,8 @@ namespace ollily\Tools\Test; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; /** @@ -20,16 +22,10 @@ */ class TestDataTest extends TestCase { - /** @var string */ - private static $fileName; + private static string $fileName; - /** - * @param int $expectedCount - * @param mixed $actual - * - * @dataProvider providerData() - */ - public function testData(int $expectedCount, $actual): void + #[DataProvider('providerData')] + public function testData(int $expectedCount, mixed $actual): void { if (is_array($actual)) { self::assertCount($expectedCount, $actual); @@ -41,7 +37,7 @@ public function testData(int $expectedCount, $actual): void /** * @return array */ - public function providerData(): array + public static function providerData(): array { return [ 'oneD' => [1, TestData::DATA_OBJECT1()], @@ -53,7 +49,7 @@ public function providerData(): array public function testConstantsKey(): void { - $expectedCount = 14; + $expectedCount = 16; $this->verifyResult($expectedCount, 'key'); } @@ -83,7 +79,7 @@ public function verifyResult(int $expectedCount, string $pivot): void * @psalm-param mixed $value * @psalm-param mixed $key */ - function ($value, $key) use ($pivot): bool { + function (mixed $value, mixed $key) use ($pivot): bool { if (is_array($key)) { return false; } else { @@ -103,6 +99,7 @@ public function testPrepareTempFile(): void self::assertFileExists(self::$fileName); } + #[Depends('testPrepareTempFile')] public function testCleanupTempFile(): void { TestData::cleanupTempFile(self::$fileName);