Skip to content

Commit 9859958

Browse files
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
1 parent ae5d770 commit 9859958

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

src/Console/ModelsCommand.php

+21-23
Original file line numberDiff line numberDiff line change
@@ -893,26 +893,18 @@ protected function createPhpDocs($class)
893893
* Get the parameters and format them correctly
894894
*
895895
* @param $method
896-
* @param bool $withTypeHint
897896
* @return array
898897
* @throws \ReflectionException
899898
*/
900-
public function getParameters($method, bool $withTypeHint = false)
899+
public function getParameters($method)
901900
{
902901
//Loop through the default values for parameters, and make the correct output string
903902
$paramsWithDefault = [];
904903
/** @var \ReflectionParameter $param */
905904
foreach ($method->getParameters() as $param) {
906-
$paramType = $param->getType();
907-
908905
$paramStr = '$' . $param->getName();
909-
if ($paramType) {
910-
$paramTypeStr = $paramType->getName();
911-
if (!$paramType->isBuiltin()) {
912-
$paramTypeStr = '\\' . $paramTypeStr;
913-
}
914-
915-
$paramStr = $paramTypeStr . ' ' . $paramStr;
906+
if ($paramType = $this->getParamType($method, $param)) {
907+
$paramStr = $paramType . ' ' . $paramStr;
916908
}
917909

918910
if ($param->isOptional() && $param->isDefaultValueAvailable()) {
@@ -932,10 +924,6 @@ public function getParameters($method, bool $withTypeHint = false)
932924
$paramStr .= " = $default";
933925
}
934926

935-
if ($withTypeHint && $paramType = $this->getParamType($method, $param)) {
936-
$paramStr = $paramType . ' ' . $paramStr;
937-
}
938-
939927
$paramsWithDefault[] = $paramStr;
940928
}
941929
return $paramsWithDefault;
@@ -1176,7 +1164,7 @@ protected function writeModelExternalBuilderMethods(string $builder, Model $mode
11761164

11771165
foreach ($newMethodsFromNewBuilder as $builderMethod) {
11781166
$reflection = new \ReflectionMethod($builder, $builderMethod);
1179-
$args = $this->getParameters($reflection, true);
1167+
$args = $this->getParameters($reflection);
11801168

11811169
$this->setMethod(
11821170
$builderMethod,
@@ -1189,11 +1177,17 @@ protected function writeModelExternalBuilderMethods(string $builder, Model $mode
11891177
protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string
11901178
{
11911179
if ($paramType = $parameter->getType()) {
1180+
$parameterName = $paramType->getName();
1181+
1182+
if (!$paramType->isBuiltin()) {
1183+
$parameterName = '\\' . $parameterName;
1184+
}
1185+
11921186
if ($paramType->allowsNull()) {
1193-
return '?' . $paramType->getName();
1187+
return '?' . $parameterName;
11941188
}
11951189

1196-
return $paramType->getName();
1190+
return $parameterName;
11971191
}
11981192

11991193
$docComment = $method->getDocComment();
@@ -1239,14 +1233,18 @@ protected function getParamType(\ReflectionMethod $method, \ReflectionParameter
12391233
$type = '?' . $type;
12401234
}
12411235

1242-
$typesThatAreNotAllowed = [
1243-
'null',
1244-
'mixed',
1245-
'nullable',
1236+
// convert to proper type hint types in php
1237+
$type = str_replace(['boolean', 'integer'], ['bool', 'int'], $type);
1238+
1239+
$allowedTypes = [
1240+
'int',
1241+
'bool',
1242+
'string',
1243+
'float',
12461244
];
12471245

12481246
// we replace the ? with an empty string so we can check the actual type
1249-
if (in_array(str_replace('?', '', $type), $typesThatAreNotAllowed)) {
1247+
if (!in_array(str_replace('?', '', $type), $allowedTypes)) {
12501248
return null;
12511249
}
12521250

tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/Builders/PostExternalQueryBuilder.php

+57
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders;
66

7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
78
use Illuminate\Database\Eloquent\Builder;
89

910
class PostExternalQueryBuilder extends Builder
@@ -32,6 +33,38 @@ public function withTheNumber($number): self
3233
return $this;
3334
}
3435

36+
/**
37+
* @param integer|null $number
38+
* @return $this
39+
*/
40+
public function withTheNumberDifferently($number): self
41+
{
42+
return $this;
43+
}
44+
45+
/**
46+
* @param bool|null $number
47+
* @return $this
48+
*/
49+
public function withBool($booleanVar): self
50+
{
51+
return $this;
52+
}
53+
54+
/**
55+
* @param bool|null $number
56+
* @return $this
57+
*/
58+
public function withBoolDifferently($booleanVar): self
59+
{
60+
return $this;
61+
}
62+
63+
public function withBoolTypeHinted(bool $booleanVar): self
64+
{
65+
return $this;
66+
}
67+
3568
/**
3669
* @param int|string $someone
3770
* @return $this
@@ -49,4 +82,28 @@ public function withMixedOption($option): self
4982
{
5083
return $this;
5184
}
85+
86+
public function withTestCommand(ModelsCommand $testCommand): self
87+
{
88+
return $this;
89+
}
90+
91+
public function withNullTestCommand(?ModelsCommand $testCommand): self
92+
{
93+
return $this;
94+
}
95+
96+
public function withNullAndAssignmentTestCommand(?ModelsCommand $testCommand = null): self
97+
{
98+
return $this;
99+
}
100+
101+
/**
102+
* @param ModelsCommand $testCommand
103+
* @return $this
104+
*/
105+
public function withNullTestCommandInDocBlock($testCommand): self
106+
{
107+
return $this;
108+
}
52109
}

tests/Console/ModelsCommand/GeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php

+8
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,17 @@
166166
* @method static \Illuminate\Database\Eloquent\Builder|Post whereUuidNullable($value)
167167
* @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNotNullable($value)
168168
* @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNullable($value)
169+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBool(?bool $booleanVar)
170+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBoolDifferently(?bool $booleanVar)
171+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withBoolTypeHinted(bool $booleanVar)
169172
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withMixedOption($option)
173+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullAndAssignmentTestCommand(?\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand = null)
174+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullTestCommand(?\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand)
175+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withNullTestCommandInDocBlock($testCommand)
170176
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withSomeone($someone)
177+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTestCommand(\Barryvdh\LaravelIdeHelper\Console\ModelsCommand $testCommand)
171178
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTheNumber(?int $number)
179+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithExternalEloquentBuilder\Builders\PostExternalQueryBuilder|Post withTheNumberDifferently(?int $number)
172180
*/
173181
class Post extends \Eloquent {}
174182
}

0 commit comments

Comments
 (0)