Skip to content

Commit

Permalink
Do not overwrite return type in normalizeReturn() with conditional ca…
Browse files Browse the repository at this point in the history
…ll of setType()
  • Loading branch information
pjio committed Sep 3, 2024
1 parent 3fa4267 commit 45d62ab
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected function addMagicMethods()
$magic,
$this->interfaces,
$this->classAliases,
$this->getReplaceReturnTypes($class)
$this->getReturnTypeNormalizers($class)
);
}
$this->usedMethods[] = $magic;
Expand Down Expand Up @@ -373,7 +373,7 @@ protected function detectMethods()
$method->name,
$this->interfaces,
$this->classAliases,
$this->getReplaceReturnTypes($reflection)
$this->getReturnTypeNormalizers($reflection)
);
}
$this->usedMethods[] = $method->name;
Expand All @@ -397,7 +397,7 @@ protected function detectMethods()
$macro_name,
$this->interfaces,
$this->classAliases,
$this->getReplaceReturnTypes($reflection)
$this->getReturnTypeNormalizers($reflection)
);
$this->usedMethods[] = $macro_name;
}
Expand All @@ -408,12 +408,12 @@ protected function detectMethods()

/**
* @param ReflectionClass $class
* @return string[]
* @return Array<string, string>
*/
protected function getReplaceReturnTypes($class)
protected function getReturnTypeNormalizers($class)
{
if ($this->alias === 'Eloquent' && in_array($class->getName(), [EloquentBuilder::class, QueryBuilder::class])) {
return ['$this' => EloquentBuilder::class . '|static'];
return ['$this' => '\\' . EloquentBuilder::class . '|static'];
}

return [];
Expand Down
6 changes: 3 additions & 3 deletions src/Macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Macro extends Method
* @param null $methodName
* @param array $interfaces
* @param array $classAliases
* @param array $replaceReturnTypes
* @param array $returnTypeNormalizers
*/
public function __construct(
$method,
Expand All @@ -27,9 +27,9 @@ public function __construct(
$methodName = null,
$interfaces = [],
$classAliases = [],
$replaceReturnTypes = []
$returnTypeNormalizers = []
) {
parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $replaceReturnTypes);
parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $returnTypeNormalizers);
}

/**
Expand Down
39 changes: 13 additions & 26 deletions src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Method
protected $return = null;
protected $root;
protected $classAliases;
protected $replaceReturnTypes;
protected $returnTypeNormalizers;

/**
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method
Expand All @@ -46,13 +46,14 @@ class Method
* @param string|null $methodName
* @param array $interfaces
* @param array $classAliases
* @param array $replaceReturnTypes
* @param array $returnTypeNormalizers
*/
public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], $replaceReturnTypes = [])
public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], array $returnTypeNormalizers = [])
{
$this->method = $method;
$this->interfaces = $interfaces;
$this->classAliases = $classAliases;
$this->returnTypeNormalizers = $returnTypeNormalizers;
$this->name = $methodName ?: $method->name;
$this->real_name = $method->isClosure() ? $this->name : $method->name;
$this->initClassDefinedProperties($method, $class);
Expand All @@ -65,7 +66,6 @@ public function __construct($method, $alias, $class, $methodName = null, $interf

//Normalize the description and inherit the docs from parents/interfaces
try {
$this->setReplaceReturnTypes($replaceReturnTypes);
$this->normalizeParams($this->phpdoc);
$this->normalizeReturn($this->phpdoc);
$this->normalizeDescription($this->phpdoc);
Expand Down Expand Up @@ -181,14 +181,6 @@ public function getParams($implode = true)
return $implode ? implode(', ', $this->params) : $this->params;
}

/**
* @return string|null
*/
public function getReturn()
{
return $this->return;
}

/**
* @param DocBlock|null $phpdoc
* @return ReturnTag|null
Expand Down Expand Up @@ -275,17 +267,8 @@ protected function normalizeParams(DocBlock $phpdoc)
}
}

protected function setReplaceReturnTypes($replaceReturnTypes)
{
if (!array_key_exists('$this', $replaceReturnTypes)) {
$replaceReturnTypes['$this'] = $this->root;
}

$this->replaceReturnTypes = $replaceReturnTypes;
}

/**
* Normalize the return tag (make full namespace, replace interfaces)
* Normalize the return tag (make full namespace, replace interfaces, resolve $this)
*
* @param DocBlock $phpdoc
*/
Expand All @@ -302,6 +285,14 @@ protected function normalizeReturn(DocBlock $phpdoc)
// Get the expanded type
$returnValue = $tag->getType();

if (array_key_exists($returnValue, $this->returnTypeNormalizers)) {
$returnValue = $this->returnTypeNormalizers[$returnValue];
}

if ($returnValue === '$this') {
$returnValue = $this->root;
}

// Replace the interfaces
foreach ($this->interfaces as $interface => $real) {
$returnValue = str_replace($interface, $real, $returnValue);
Expand All @@ -310,10 +301,6 @@ protected function normalizeReturn(DocBlock $phpdoc)
// Set the changed content
$tag->setContent($returnValue . ' ' . $tag->getDescription());
$this->return = $returnValue;

if (array_key_exists($tag->getType(), $this->replaceReturnTypes)) {
$tag->setType($this->replaceReturnTypes[$tag->getType()]);
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testEloquentBuilderOutput()
$reflectionClass = new \ReflectionClass(EloquentBuilder::class);
$reflectionMethod = $reflectionClass->getMethod('with');

$method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => EloquentBuilder::class . '|static']);
$method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']);

$expectedDocComment = <<<'DOC'
/**
Expand All @@ -80,7 +80,6 @@ public function testEloquentBuilderOutput()
$this->assertSame(['$relations', '$callback'], $method->getParams(false));
$this->assertSame(['$relations', '$callback = null'], $method->getParamsWithDefault(false));
$this->assertTrue($method->shouldReturn());
$this->assertSame('$this', $method->getReturn());
$this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType()));
}

Expand All @@ -92,7 +91,7 @@ public function testQueryBuilderOutput()
$reflectionClass = new \ReflectionClass(QueryBuilder::class);
$reflectionMethod = $reflectionClass->getMethod('whereNull');

$method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => EloquentBuilder::class . '|static']);
$method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']);

$expectedDocComment = <<<'DOC'
/**
Expand All @@ -112,7 +111,6 @@ public function testQueryBuilderOutput()
$this->assertSame(['$columns', '$boolean', '$not'], $method->getParams(false));
$this->assertSame(['$columns', "\$boolean = 'and'", '$not = false'], $method->getParamsWithDefault(false));
$this->assertTrue($method->shouldReturn());
$this->assertSame('$this', $method->getReturn());
$this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType()));
}

Expand Down

0 comments on commit 45d62ab

Please sign in to comment.