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
*
* @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;
}
@@ -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;
}
}
@@ -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 {}
}