Skip to content

Commit ce0b89d

Browse files
author
Andrey Helldar
committed
Added methods for getting summary and description of methods
1 parent f51ab84 commit ce0b89d

File tree

8 files changed

+262
-78
lines changed

8 files changed

+262
-78
lines changed

composer.json

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"require": {
1818
"php": "^7.2",
1919
"andrey-helldar/support": "^1.24",
20-
"doctrine/annotations": "^1.0",
2120
"illuminate/routing": "^5.0|^6.0|^7.0|^8.0",
22-
"illuminate/support": "^5.0|^6.0|^7.0|^8.0"
21+
"illuminate/support": "^5.0|^6.0|^7.0|^8.0",
22+
"phpdocumentor/reflection-docblock": "^5.0"
2323
},
2424
"require-dev": {
2525
"mockery/mockery": "^0.9|^1.0",
@@ -41,5 +41,25 @@
4141
"sort-packages": true
4242
},
4343
"minimum-stability": "stable",
44-
"prefer-stable": true
44+
"prefer-stable": true,
45+
"extra": {
46+
"thanks": [
47+
{
48+
"name": "andrey-helldar/support",
49+
"url": "https://github.com/andrey-helldar/support"
50+
},
51+
{
52+
"name": "illuminate/routing",
53+
"url": "https://github.com/illuminate/routing"
54+
},
55+
{
56+
"name": "illuminate/support",
57+
"url": "https://github.com/illuminate/support"
58+
},
59+
{
60+
"name": "phpdocumentor/reflection-docblock",
61+
"url": "https://github.com/phpDocumentor/ReflectionDocBlock"
62+
}
63+
]
64+
}
4565
}

src/Models/Route.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ public function getMiddlewares(): array
123123
return array_values($middlewares);
124124
}
125125

126+
public function getSummary(): ?string
127+
{
128+
return Annotation::summary($this->getAction());
129+
}
130+
131+
public function getDescription(): ?string
132+
{
133+
return Annotation::description($this->getAction());
134+
}
135+
126136
public function getDeprecated(): bool
127137
{
128138
return Annotation::isDeprecated($this->getAction());
@@ -140,6 +150,8 @@ public function toArray()
140150
'action' => $this->getAction(),
141151
'middlewares' => $this->getMiddlewares(),
142152
'deprecated' => $this->getDeprecated(),
153+
'summary' => $this->getSummary(),
154+
'description' => $this->getDescription(),
143155
];
144156
}
145157
}

src/Support/Annotation.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,42 @@
22

33
namespace Helldar\LaravelRoutesCore\Support;
44

5-
use Doctrine\Common\Annotations\AnnotationReader;
65
use Illuminate\Support\Str;
6+
use phpDocumentor\Reflection\DocBlock;
7+
use phpDocumentor\Reflection\DocBlockFactory;
78
use ReflectionClass;
89

910
final class Annotation
1011
{
12+
/**
13+
* @param string $controller
14+
* @param string|null $method
15+
*
16+
* @throws \ReflectionException
17+
*
18+
* @return string|null
19+
*/
1120
public function summary(string $controller, string $method = null): ?string
1221
{
13-
if ($comment = $this->docComment($controller, $method)) {
14-
return $comment;
22+
if ($reader = $this->reader($controller, $method)) {
23+
return $reader->getSummary() ?: null;
1524
}
1625

1726
return null;
1827
}
1928

29+
/**
30+
* @param string $controller
31+
* @param string|null $method
32+
*
33+
* @throws \ReflectionException
34+
*
35+
* @return string|null
36+
*/
2037
public function description(string $controller, string $method = null): ?string
2138
{
22-
if ($comment = $this->docComment($controller, $method)) {
23-
return $comment;
39+
if ($reader = $this->reader($controller, $method)) {
40+
return $reader->getDescription()->getBodyTemplate() ?: null;
2441
}
2542

2643
return null;
@@ -38,11 +55,9 @@ public function description(string $controller, string $method = null): ?string
3855
*/
3956
public function isDeprecated(string $controller, string $method = null)
4057
{
41-
if (is_null($method)) {
42-
[$controller, $method] = $this->parse($controller);
43-
}
44-
45-
return $this->isDeprecatedClass($controller) || $this->isDeprecatedMethod($controller, $method);
58+
return is_null($method)
59+
? $this->isDeprecatedClass($controller)
60+
: $this->isDeprecatedMethod($controller, $method);
4661
}
4762

4863
/**
@@ -55,10 +70,10 @@ public function isDeprecated(string $controller, string $method = null)
5570
*
5671
* @return bool
5772
*/
58-
public function isDeprecatedMethod(string $controller, string $method)
73+
public function isDeprecatedMethod(string $controller, string $method): bool
5974
{
60-
if ($item = $this->getReflectionMethod($controller, $method)) {
61-
return $this->contains($item->getDocComment());
75+
if ($reader = $this->reader($controller, $method)) {
76+
return $reader->hasTag('@deprecated');
6277
}
6378

6479
return false;
@@ -75,8 +90,8 @@ public function isDeprecatedMethod(string $controller, string $method)
7590
*/
7691
public function isDeprecatedClass(string $controller)
7792
{
78-
if ($item = $this->reflectionClass($controller)) {
79-
return $this->contains($item->getDocComment());
93+
if ($reader = $this->reader($controller)) {
94+
return $reader->hasTag('deprecated');
8095
}
8196

8297
return false;
@@ -98,25 +113,41 @@ protected function parse(string $action)
98113
}
99114

100115
/**
101-
* Determines if an deprecated method label exists in a string.
116+
* @param string $controller
117+
* @param string|null $method
102118
*
103-
* @param $haystack
119+
* @throws \ReflectionException
104120
*
105-
* @return bool
121+
* @return \phpDocumentor\Reflection\DocBlock|null
106122
*/
107-
protected function contains($haystack)
123+
protected function reader(string $controller, string $method = null): ?DocBlock
108124
{
109-
return Str::contains($haystack, '@deprecated');
125+
if (is_null($method)) {
126+
[$controller, $method] = $this->parse($controller);
127+
}
128+
129+
$item = $this->reflection($controller, $method);
130+
131+
if ($item && $comment = $item->getDocComment()) {
132+
return DocBlockFactory::createInstance()->create($comment);
133+
}
134+
135+
return null;
110136
}
111137

112138
/**
113-
* Annotation Reader Instance.
139+
* @param string $controller
140+
* @param string|null $method
141+
*
142+
* @throws \ReflectionException
114143
*
115-
* @return \Doctrine\Common\Annotations\AnnotationReader
144+
* @return \ReflectionClass|\ReflectionMethod
116145
*/
117-
protected function reader()
146+
protected function reflection(string $controller, string $method = null)
118147
{
119-
return new AnnotationReader();
148+
$class = $this->reflectionClass($controller);
149+
150+
return is_null($method) ? $class : $this->reflectionMethod($class, $method);
120151
}
121152

122153
/**
@@ -149,35 +180,4 @@ protected function reflectionMethod(ReflectionClass $class, string $method)
149180
? $class->getMethod($method)
150181
: null;
151182
}
152-
153-
/**
154-
* Getting class reflection instance.
155-
*
156-
* @param string $controller
157-
* @param string $method
158-
*
159-
* @throws \ReflectionException
160-
*
161-
* @return \ReflectionMethod|null
162-
*/
163-
protected function getReflectionMethod(string $controller, string $method)
164-
{
165-
return $this->reflectionMethod(
166-
$this->reflectionClass($controller),
167-
$method
168-
);
169-
}
170-
171-
protected function docComment(string $controller, string $method = null): ?string
172-
{
173-
if (is_null($method)) {
174-
[$controller, $method] = $this->parse($controller);
175-
}
176-
177-
$class = $this->reflectionClass($controller);
178-
179-
$item = is_null($method) ? $class : $this->reflectionMethod($class, $method);
180-
181-
return $item->getDocComment() ?: null;
182-
}
183183
}

tests/AnnotationsTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Helldar\LaravelRoutesCore\Facades\Routes;
6+
use Helldar\LaravelRoutesCore\Models\Route;
7+
8+
final class AnnotationsTest extends TestCase
9+
{
10+
public function testSummary()
11+
{
12+
$route = $this->route('summary');
13+
14+
$this->assertEquals('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.', $route->getSummary());
15+
$this->assertNull($route->getDescription());
16+
$this->assertFalse($route->getDeprecated());
17+
}
18+
19+
public function testDescription()
20+
{
21+
$route = $this->route('description');
22+
23+
$this->assertEquals('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.', $route->getSummary());
24+
25+
$this->assertEquals(
26+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" .
27+
"Pellentesque lorem libero, ultricies ut nisl in, vestibulum egestas neque.\n" .
28+
"Nulla facilisi. Aenean vitae justo bibendum, scelerisque arcu cursus, scelerisque sapien.",
29+
$route->getDescription());
30+
31+
$this->assertFalse($route->getDeprecated());
32+
}
33+
34+
public function testDeprecated()
35+
{
36+
$route = $this->route('deprecated');
37+
38+
$this->assertEquals('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.', $route->getSummary());
39+
40+
$this->assertEquals(
41+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" .
42+
"Pellentesque lorem libero, ultricies ut nisl in, vestibulum egestas neque.\n" .
43+
"Nulla facilisi. Aenean vitae justo bibendum, scelerisque arcu cursus, scelerisque sapien.",
44+
$route->getDescription());
45+
46+
$this->assertTrue($route->getDeprecated());
47+
}
48+
49+
public function testWithout()
50+
{
51+
$route = $this->route('without');
52+
53+
$this->assertNull($route->getSummary());
54+
$this->assertNull($route->getDescription());
55+
56+
$this->assertFalse($route->getDeprecated());
57+
}
58+
59+
public function withoutDeprecated()
60+
{
61+
$route = $this->route('withoutDeprecated');
62+
63+
$this->assertNull($route->getSummary());
64+
$this->assertNull($route->getDescription());
65+
66+
$this->assertTrue($route->getDeprecated());
67+
}
68+
69+
protected function route(string $name): Route
70+
{
71+
return Routes::collection()
72+
->filter(static function (Route $route) use ($name) {
73+
return $route->getName() === $name;
74+
})->first();
75+
}
76+
}

tests/Fixtures/Controller.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
final class Controller
6+
{
7+
/**
8+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.
9+
*/
10+
public function summary()
11+
{
12+
13+
}
14+
15+
/**
16+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.
17+
*
18+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
19+
* Pellentesque lorem libero, ultricies ut nisl in, vestibulum egestas neque.
20+
* Nulla facilisi. Aenean vitae justo bibendum, scelerisque arcu cursus, scelerisque sapien.
21+
*/
22+
public function description()
23+
{
24+
}
25+
26+
/**
27+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse justo.
28+
*
29+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
30+
* Pellentesque lorem libero, ultricies ut nisl in, vestibulum egestas neque.
31+
* Nulla facilisi. Aenean vitae justo bibendum, scelerisque arcu cursus, scelerisque sapien.
32+
*
33+
* @deprecated
34+
*/
35+
public function deprecated()
36+
{
37+
38+
}
39+
40+
/**
41+
* @return void
42+
*/
43+
public function without()
44+
{
45+
}
46+
47+
/**
48+
* @deprecated
49+
*
50+
* @return void
51+
*/
52+
public function withoutDeprecated()
53+
{
54+
55+
}
56+
}

tests/Fixtures/ServiceProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6+
7+
final class ServiceProvider extends BaseServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->loadRoutesFrom(__DIR__ . '/routes.php');
12+
}
13+
}

0 commit comments

Comments
 (0)