From 9859958cfaf522b8e6ef13352067f9ef82f9e8b5 Mon Sep 17 00:00:00 2001 From: Kaloqn <32775332+KaloyanYosifov@users.noreply.github.com> Date: Fri, 4 Dec 2020 15:23:42 +0200 Subject: [PATCH] Fix broken test "GeneratePhpdocWithExternalEloquentBuilder" (#1111) * Fix code that was causing the test to break and fix bug with not built in types * Run cs fixer * Remove fetching the doc block type and the putting it as a type hint as a php doc block is sufficient * Revert doc block type hinting * Enchance doc block type hinting * Combine replacements --- src/Console/ModelsCommand.php | 46 +++++++-------- .../Builders/PostExternalQueryBuilder.php | 57 +++++++++++++++++++ .../__snapshots__/Test__test__1.php | 8 +++ 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 4a21a43..69c95b9 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -893,26 +893,18 @@ class ModelsCommand extends Command * Get the parameters and format them correctly * * @param $method - * @param bool $withTypeHint * @return array * @throws \ReflectionException */ - public function getParameters($method, bool $withTypeHint = false) + public function getParameters($method) { //Loop through the default values for parameters, and make the correct output string $paramsWithDefault = []; /** @var \ReflectionParameter $param */ foreach ($method->getParameters() as $param) { - $paramType = $param->getType(); - $paramStr = '$' . $param->getName(); - if ($paramType) { - $paramTypeStr = $paramType->getName(); - if (!$paramType->isBuiltin()) { - $paramTypeStr = '\\' . $paramTypeStr; - } - - $paramStr = $paramTypeStr . ' ' . $paramStr; + if ($paramType = $this->getParamType($method, $param)) { + $paramStr = $paramType . ' ' . $paramStr; } if ($param->isOptional() && $param->isDefaultValueAvailable()) { @@ -932,10 +924,6 @@ class ModelsCommand extends Command $paramStr .= " = $default"; } - if ($withTypeHint && $paramType = $this->getParamType($method, $param)) { - $paramStr = $paramType . ' ' . $paramStr; - } - $paramsWithDefault[] = $paramStr; } return $paramsWithDefault; @@ -1176,7 +1164,7 @@ class ModelsCommand extends Command foreach ($newMethodsFromNewBuilder as $builderMethod) { $reflection = new \ReflectionMethod($builder, $builderMethod); - $args = $this->getParameters($reflection, true); + $args = $this->getParameters($reflection); $this->setMethod( $builderMethod, @@ -1189,11 +1177,17 @@ class ModelsCommand extends Command protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string { if ($paramType = $parameter->getType()) { - if ($paramType->allowsNull()) { - return '?' . $paramType->getName(); + $parameterName = $paramType->getName(); + + if (!$paramType->isBuiltin()) { + $parameterName = '\\' . $parameterName; } - return $paramType->getName(); + if ($paramType->allowsNull()) { + return '?' . $parameterName; + } + + return $parameterName; } $docComment = $method->getDocComment(); @@ -1239,14 +1233,18 @@ class ModelsCommand extends Command $type = '?' . $type; } - $typesThatAreNotAllowed = [ - 'null', - 'mixed', - 'nullable', + // convert to proper type hint types in php + $type = str_replace(['boolean', 'integer'], ['bool', 'int'], $type); + + $allowedTypes = [ + 'int', + 'bool', + 'string', + 'float', ]; // we replace the ? with an empty string so we can check the actual type - if (in_array(str_replace('?', '', $type), $typesThatAreNotAllowed)) { + if (!in_array(str_replace('?', '', $type), $allowedTypes)) { return null; } diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/Builders/PostExternalQueryBuilder.php b/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/Builders/PostExternalQueryBuilder.php index cab251f..2084532 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/Builders/PostExternalQueryBuilder.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/Builders/PostExternalQueryBuilder.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders; +use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Illuminate\Database\Eloquent\Builder; class PostExternalQueryBuilder extends Builder @@ -32,6 +33,38 @@ class PostExternalQueryBuilder extends Builder return $this; } + /** + * @param integer|null $number + * @return $this + */ + public function withTheNumberDifferently($number): self + { + return $this; + } + + /** + * @param bool|null $number + * @return $this + */ + public function withBool($booleanVar): self + { + return $this; + } + + /** + * @param bool|null $number + * @return $this + */ + public function withBoolDifferently($booleanVar): self + { + return $this; + } + + public function withBoolTypeHinted(bool $booleanVar): self + { + return $this; + } + /** * @param int|string $someone * @return $this @@ -49,4 +82,28 @@ class PostExternalQueryBuilder extends Builder { return $this; } + + public function withTestCommand(ModelsCommand $testCommand): self + { + return $this; + } + + public function withNullTestCommand(?ModelsCommand $testCommand): self + { + return $this; + } + + public function withNullAndAssignmentTestCommand(?ModelsCommand $testCommand = null): self + { + return $this; + } + + /** + * @param ModelsCommand $testCommand + * @return $this + */ + public function withNullTestCommandInDocBlock($testCommand): self + { + return $this; + } } diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php index 4ba3bcf..68d935d 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php @@ -166,9 +166,17 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWi * @method static \Illuminate\Database\Eloquent\Builder|Post whereUuidNullable($value) * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNotNullable($value) * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNullable($value) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBool(?bool $booleanVar) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBoolDifferently(?bool $booleanVar) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBoolTypeHinted(bool $booleanVar) * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withMixedOption($option) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullAndAssignmentTestCommand(?\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand = null) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullTestCommand(?\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullTestCommandInDocBlock($testCommand) * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withSomeone($someone) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTestCommand(\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand) * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTheNumber(?int $number) + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTheNumberDifferently(?int $number) */ class Post extends \Eloquent {} }