Skip to content

Commit

Permalink
Fixed TypeError: Argument #1 ($tag) must be of type DocBlock\Tags\Ret…
Browse files Browse the repository at this point in the history
…urn_, DocBlock\Tags\InvalidTag given
  • Loading branch information
Andrey Helldar committed Oct 13, 2021
1 parent 206a10b commit 106533b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/Support/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Helldar\LaravelRoutesCore\Models\Reader;
use Helldar\LaravelRoutesCore\Models\Tags\Returns;
use Helldar\LaravelRoutesCore\Models\Tags\Throws;
use Helldar\Support\Facades\Helpers\Instance;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use phpDocumentor\Reflection\DocBlock;
Expand Down Expand Up @@ -58,10 +59,10 @@ public function isDeprecated(string $controller, string $method = null)
*/
public function exceptions(string $controller, string $method = null): Collection
{
$callback = static function (DocBlock $doc) {
$callback = function (DocBlock $doc) {
return array_map(static function (DocBlock\Tags\Throws $tag) {
return Throws::make($tag);
}, $doc->getTagsByName('throws'));
}, $this->getTagsByName($doc, 'throws'));
};

$reader = $this->reader($controller, $method);
Expand All @@ -85,10 +86,10 @@ public function exceptions(string $controller, string $method = null): Collectio
*/
public function response(string $controller, string $method = null): ?Returns
{
return $this->get(static function (DocBlock $doc) {
return $this->get(function (DocBlock $doc) {
$returns = array_map(static function (DocBlock\Tags\Return_ $tag) {
return Returns::make($tag);
}, $doc->getTagsByName('return'));
}, $this->getTagsByName($doc, 'return'));

return Arr::first($returns);
}, $controller, $method);
Expand Down Expand Up @@ -117,4 +118,11 @@ protected function getValue(callable $callback, Reader $reader, string $method,

return $default;
}

protected function getTagsByName(DocBlock $doc, string $name): array
{
return array_filter($doc->getTagsByName($name), static function ($tag) {
return ! Instance::of($tag, DocBlock\Tags\InvalidTag::class);
});
}
}
7 changes: 7 additions & 0 deletions tests/Fixtures/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public function routeWebMiddleware()
public function controllerWebMiddleware()
{
}

/**
* @return
*/
public function incorrectDocBlock()
{
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
app('router')->get('deprecated', '\Tests\Fixtures\Controller@deprecated')->name('deprecated');
app('router')->get('without', '\Tests\Fixtures\Controller@without')->name('without');
app('router')->get('withoutDeprecated', '\Tests\Fixtures\Controller@withoutDeprecated')->name('withoutDeprecated');
app('router')->get('incorrectDocBlock', '\Tests\Fixtures\Controller@incorrectDocBlock')->name('incorrectDocBlock');

app('router')
->middleware('api')
Expand Down
63 changes: 57 additions & 6 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public function testMapping()
$this->routeWithoutDeprecated($route);
break;

case 'incorrectDocBlock':
$this->routeIncorrectDocBlock($route);
break;

case 'routeApiMiddleware':
$this->routeRoutingApiMiddleware($route);
break;
Expand Down Expand Up @@ -177,6 +181,10 @@ public function testConfig()
$this->routeWithoutDeprecated($route);
break;

case 'incorrectDocBlock':
$this->routeIncorrectDocBlock($route);
break;

case 'routeApiMiddleware':
$this->routeRoutingApiMiddleware($route);
break;
Expand Down Expand Up @@ -641,7 +649,7 @@ protected function routeWithoutDeprecated(Route $route)
$this->assertFalse($route->isWeb());
}

protected function routeRoutingApiMiddleware(Route $route)
protected function routeIncorrectDocBlock(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(11, $route->getPriority());
Expand All @@ -651,6 +659,49 @@ protected function routeRoutingApiMiddleware(Route $route)

$this->assertNull($route->getDomain());

$this->assertIsString($route->getPath());
$this->assertSame('incorrectDocBlock', $route->getPath());

$this->assertIsString($route->getName());
$this->assertSame('incorrectDocBlock', $route->getName());

$this->assertNull($route->getModule());

$this->assertIsString($route->getAction());
$this->assertSame('Tests\Fixtures\Controller@incorrectDocBlock', $route->getAction());

$this->assertIsArray($route->getMiddlewares());
$this->assertSame([], $route->getMiddlewares());

$this->assertIsBool($route->getDeprecated());
$this->assertFalse($route->getDeprecated());

$this->assertNull($route->getSummary());
$this->assertNull($route->getDescription());

$this->assertIsArray($route->getExceptions()->toArray());
$this->assertSame([], $route->getExceptions()->toArray());
$this->assertTrue($route->getExceptions()->isEmpty());

$this->assertNull($route->getResponse());

$this->assertIsBool($route->isApi());
$this->assertFalse($route->isApi());

$this->assertIsBool($route->isWeb());
$this->assertFalse($route->isWeb());
}

protected function routeRoutingApiMiddleware(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(12, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());

$this->assertNull($route->getDomain());

$this->assertIsString($route->getPath());
$this->assertSame('routeApiMiddleware', $route->getPath());

Expand Down Expand Up @@ -687,7 +738,7 @@ protected function routeRoutingApiMiddleware(Route $route)
protected function routeControllerApiMiddleware(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(12, $route->getPriority());
$this->assertSame(13, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
Expand Down Expand Up @@ -730,7 +781,7 @@ protected function routeControllerApiMiddleware(Route $route)
protected function routeRoutingWebMiddleware(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(13, $route->getPriority());
$this->assertSame(14, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
Expand Down Expand Up @@ -772,7 +823,7 @@ protected function routeRoutingWebMiddleware(Route $route)
protected function routeControllerWebMiddleware(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(14, $route->getPriority());
$this->assertSame(15, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
Expand Down Expand Up @@ -815,7 +866,7 @@ protected function routeControllerWebMiddleware(Route $route)
protected function routeClosureNullName(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(15, $route->getPriority());
$this->assertSame(16, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
Expand Down Expand Up @@ -856,7 +907,7 @@ protected function routeClosureNullName(Route $route)
protected function routeClosure(Route $route)
{
$this->assertIsInt($route->getPriority());
$this->assertSame(16, $route->getPriority());
$this->assertSame(17, $route->getPriority());

$this->assertIsArray($route->getMethods());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
Expand Down

0 comments on commit 106533b

Please sign in to comment.