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
This commit is contained in:
Kaloqn
2020-12-04 14:23:42 +01:00
committed by GitHub
parent ae5d7708b7
commit 9859958cfa
3 changed files with 87 additions and 24 deletions
+22 -24
View File
@@ -893,26 +893,18 @@ class ModelsCommand extends Command
* Get the parameters and format them correctly * Get the parameters and format them correctly
* *
* @param $method * @param $method
* @param bool $withTypeHint
* @return array * @return array
* @throws \ReflectionException * @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 //Loop through the default values for parameters, and make the correct output string
$paramsWithDefault = []; $paramsWithDefault = [];
/** @var \ReflectionParameter $param */ /** @var \ReflectionParameter $param */
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$paramType = $param->getType();
$paramStr = '$' . $param->getName(); $paramStr = '$' . $param->getName();
if ($paramType) { if ($paramType = $this->getParamType($method, $param)) {
$paramTypeStr = $paramType->getName(); $paramStr = $paramType . ' ' . $paramStr;
if (!$paramType->isBuiltin()) {
$paramTypeStr = '\\' . $paramTypeStr;
}
$paramStr = $paramTypeStr . ' ' . $paramStr;
} }
if ($param->isOptional() && $param->isDefaultValueAvailable()) { if ($param->isOptional() && $param->isDefaultValueAvailable()) {
@@ -932,10 +924,6 @@ class ModelsCommand extends Command
$paramStr .= " = $default"; $paramStr .= " = $default";
} }
if ($withTypeHint && $paramType = $this->getParamType($method, $param)) {
$paramStr = $paramType . ' ' . $paramStr;
}
$paramsWithDefault[] = $paramStr; $paramsWithDefault[] = $paramStr;
} }
return $paramsWithDefault; return $paramsWithDefault;
@@ -1176,7 +1164,7 @@ class ModelsCommand extends Command
foreach ($newMethodsFromNewBuilder as $builderMethod) { foreach ($newMethodsFromNewBuilder as $builderMethod) {
$reflection = new \ReflectionMethod($builder, $builderMethod); $reflection = new \ReflectionMethod($builder, $builderMethod);
$args = $this->getParameters($reflection, true); $args = $this->getParameters($reflection);
$this->setMethod( $this->setMethod(
$builderMethod, $builderMethod,
@@ -1189,11 +1177,17 @@ class ModelsCommand extends Command
protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string
{ {
if ($paramType = $parameter->getType()) { if ($paramType = $parameter->getType()) {
if ($paramType->allowsNull()) { $parameterName = $paramType->getName();
return '?' . $paramType->getName();
if (!$paramType->isBuiltin()) {
$parameterName = '\\' . $parameterName;
} }
return $paramType->getName(); if ($paramType->allowsNull()) {
return '?' . $parameterName;
}
return $parameterName;
} }
$docComment = $method->getDocComment(); $docComment = $method->getDocComment();
@@ -1239,14 +1233,18 @@ class ModelsCommand extends Command
$type = '?' . $type; $type = '?' . $type;
} }
$typesThatAreNotAllowed = [ // convert to proper type hint types in php
'null', $type = str_replace(['boolean', 'integer'], ['bool', 'int'], $type);
'mixed',
'nullable', $allowedTypes = [
'int',
'bool',
'string',
'float',
]; ];
// we replace the ? with an empty string so we can check the actual type // 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; return null;
} }
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders; namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
class PostExternalQueryBuilder extends Builder class PostExternalQueryBuilder extends Builder
@@ -32,6 +33,38 @@ class PostExternalQueryBuilder extends Builder
return $this; 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 * @param int|string $someone
* @return $this * @return $this
@@ -49,4 +82,28 @@ class PostExternalQueryBuilder extends Builder
{ {
return $this; 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;
}
} }
@@ -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 whereUuidNullable($value)
* @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNotNullable($value) * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNotNullable($value)
* @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNullable($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 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 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 withTheNumber(?int $number)
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTheNumberDifferently(?int $number)
*/ */
class Post extends \Eloquent {} class Post extends \Eloquent {}
} }