diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index e4bcb543..6fb01644 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -84,9 +84,6 @@ jobs: - name: "Run static code analysis with phpstan/phpstan in generator/ directory" run: "composer phpstan" working-directory: "generator" - - name: "Run static code analysis with rector/rector in generator/ directory" - run: "composer rector --dry-run" - working-directory: "generator" generated_tests: name: "Generated Tests" diff --git a/generator/src/GenerateCommand.php b/generator/src/GenerateCommand.php index 4868a29d..572e5bd4 100644 --- a/generator/src/GenerateCommand.php +++ b/generator/src/GenerateCommand.php @@ -111,9 +111,9 @@ private function runCsFix(OutputInterface $output): void $process->setTimeout(600); $process->run(function ($type, $buffer) use ($output) { if (Process::ERR === $type) { - echo $output->write(''.$buffer.''); + $output->write(''.$buffer.''); } else { - echo $output->write($buffer); + $output->write($buffer); } }); } diff --git a/generator/src/Method.php b/generator/src/Method.php index 3e610500..592a08e4 100644 --- a/generator/src/Method.php +++ b/generator/src/Method.php @@ -177,7 +177,7 @@ private function removeString(string $string, string $search): string private function getStringForXPath(string $xpath): string { $paragraphs = $this->rootEntity->xpath($xpath); - if ($paragraphs === false) { + if ($paragraphs === false || $paragraphs === null) { throw new \RuntimeException('Error while performing Xpath request.'); } $str = ''; diff --git a/generator/src/PhpStanFunctions/PhpStanType.php b/generator/src/PhpStanFunctions/PhpStanType.php index b7b14b96..401ca8ec 100644 --- a/generator/src/PhpStanFunctions/PhpStanType.php +++ b/generator/src/PhpStanFunctions/PhpStanType.php @@ -59,7 +59,7 @@ public function __construct(string $data, bool $writeOnly = false) /** @var int $count */ $count = \count($returnTypes); if ($count === 0) { - throw new \RuntimeException('Error when trying to extract parameter type'); + $returnType = ''; } foreach ($returnTypes as &$returnType) { $pos = \strpos($returnType, '?'); @@ -67,21 +67,41 @@ public function __construct(string $data, bool $writeOnly = false) $nullable = true; $returnType = \str_replace('?', '', $returnType); } - //remove the parenthesis only if we are not dealing with a callable - if (\strpos($returnType, 'callable') === false) { + // remove the parenthesis only if we are not dealing with a callable + if (!str_contains($returnType, 'callable')) { $returnType = \str_replace(['(', ')'], '', $returnType); } - //here we deal with some weird phpstan typings - if ($returnType === 'non-empty-string') { + + // here we deal with some weird phpstan typings + if (str_contains($returnType, 'non-falsy-string')) { $returnType = 'string'; - } elseif ($returnType === 'positive-int') { + } + + if (str_contains($returnType, 'non-empty-string')) { + $returnType = 'string'; + } + + if (str_contains($returnType, '__stringAndStringable')) { + $returnType = 'string'; + } + + if ($returnType === 'positive-int') { $returnType = 'int'; } elseif (is_numeric($returnType)) { $returnType = 'int'; } - if (\strpos($returnType, 'list<') !== false) { + if (str_contains($returnType, 'list<')) { $returnType = \str_replace('list', 'array', $returnType); } + + if (str_contains($returnType, 'int<')) { + $returnType = 'int'; + } + + if (\preg_match('/__benevolent\<(.*)\>/', $returnType, $regs)) { + $returnType = $regs[1]; + } + $returnType = Type::toRootNamespace($returnType); } $this->types = array_unique($returnTypes); @@ -114,7 +134,7 @@ public function getSignatureType(?int $errorType = null): string $falsable = $errorType === Method::FALSY_TYPE ? false : $this->falsable; $types = $this->types; //no typehint exists for thoses cases - if (\array_intersect(self::NO_SIGNATURE_TYPES, $types)) { + if (\array_intersect(self::NO_SIGNATURE_TYPES, $types) !== []) { return ''; } @@ -135,12 +155,14 @@ public function getSignatureType(?int $errorType = null): string //if there are several distinct types, no typehint (we use distinct in case doc block contains several times the same type, for example array|array) if (count(array_unique($types)) > 1) { return ''; - } elseif (\in_array('void', $types) || (count($types) === 0 && !$nullable && !$falsable)) { + } + + if (\in_array('void', $types) || ($types === [] && !$nullable && !$falsable)) { return 'void'; } - $finalType = $types[0]; + $finalType = $types[0] ?? ''; if ($finalType === 'bool' && !$nullable && $errorType === Method::FALSY_TYPE) { // If the function only returns a boolean, since false is for error, true is for success. // Let's replace this with a "void". diff --git a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php index 4b6ff87d..79fac21e 100644 --- a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php +++ b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php @@ -112,7 +112,11 @@ public function testVoid(): void { $param = new PhpStanType(''); $this->assertEquals('', $param->getDocBlockType()); - $this->assertEquals('', $param->getSignatureType()); + if (PHP_VERSION_ID >= 80200) { + $this->assertEquals('void', $param->getSignatureType()); + } else { + $this->assertEquals('', $param->getSignatureType()); + } $param = new PhpStanType('void'); $this->assertEquals('void', $param->getDocBlockType());