Skip to content

Commit 0916881

Browse files
authored
Merge pull request #150 from hans-thomas/refactoring-resolveClassMethodDependencies-method
Refactoring resolveClassMethodDependencies method
2 parents 07aab91 + bc24820 commit 0916881

File tree

4 files changed

+109
-21
lines changed

4 files changed

+109
-21
lines changed

src/Services/SwaggerService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,12 @@ public function getConcreteRequest()
646646
$class = $explodedController[0];
647647
$method = $explodedController[1];
648648

649-
$instance = app($class);
650-
$route = $this->request->route();
649+
if (!method_exists($class, $method)) {
650+
return null;
651+
}
651652

652653
$parameters = $this->resolveClassMethodDependencies(
653-
$route->parametersWithoutNulls(),
654-
$instance,
654+
app($class),
655655
$method
656656
);
657657

src/Traits/GetDependenciesTrait.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,22 @@
1111

1212
trait GetDependenciesTrait
1313
{
14-
protected function resolveClassMethodDependencies(array $parameters, $instance, $method)
15-
{
16-
if (!method_exists($instance, $method)) {
17-
return $parameters;
18-
}
19-
20-
return $this->getDependencies(
21-
new ReflectionMethod($instance, $method)
22-
);
23-
}
24-
25-
public function getDependencies(ReflectionFunctionAbstract $reflector)
14+
public function resolveClassMethodDependencies(object $instance, string $method): array
2615
{
2716
return array_map(function ($parameter) {
2817
return $this->transformDependency($parameter);
29-
}, $reflector->getParameters());
18+
}, (new ReflectionMethod($instance, $method))->getParameters());
3019
}
3120

3221
protected function transformDependency(ReflectionParameter $parameter)
3322
{
34-
$class = $parameter->getClass();
23+
$type = $parameter->getType();
3524

36-
if (empty($class)) {
25+
if (empty($type)) {
3726
return null;
3827
}
3928

40-
return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name;
29+
return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->getName();
4130
}
4231

4332
protected function getClassByInterface($interfaceName)

tests/SwaggerServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ public function testAddDataPostRequestWithObjectParams()
687687

688688
public function testAddDataWithNotExistsMethodOnController()
689689
{
690-
$this->mockDriverGetTmpData($this->getJsonFixture('tmp_data_get_user_request'));
690+
$this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_get_user_request_without_request_class'));
691691

692692
$service = app(SwaggerService::class);
693693

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"openapi": "3.1.0",
3+
"servers": [
4+
{
5+
"url": "http:\/\/localhost"
6+
}
7+
],
8+
"paths": {
9+
"/users/{id}/assign-role/{role-id}": {
10+
"get": {
11+
"tags": [
12+
"users"
13+
],
14+
"consumes": [],
15+
"produces": [
16+
"application/json"
17+
],
18+
"parameters": [
19+
{
20+
"in": "path",
21+
"name": "id",
22+
"description": "",
23+
"required": true,
24+
"schema": {
25+
"type": "string"
26+
}
27+
},
28+
{
29+
"in": "path",
30+
"name": "role-id",
31+
"description": "",
32+
"required": true,
33+
"schema": {
34+
"type": "string"
35+
}
36+
}
37+
],
38+
"responses": {
39+
"200": {
40+
"description": "OK",
41+
"content": {
42+
"application/json": {
43+
"schema": {
44+
"$ref": "#/components/schemas/getUsers{id}assignRole{roleId}200ResponseObject",
45+
"type": "object"
46+
},
47+
"example": {
48+
"id": 2,
49+
"name": "first_client",
50+
"likes_count": 23,
51+
"role": {
52+
"id": 2,
53+
"name": "client"
54+
},
55+
"type": "reader"
56+
}
57+
}
58+
}
59+
}
60+
},
61+
"security": [],
62+
"description": ""
63+
}
64+
}
65+
},
66+
"components": {
67+
"schemas": {
68+
"getUsers{id}assignRole{roleId}200ResponseObject": {
69+
"type": "object",
70+
"properties": {
71+
"id": {
72+
"type": "integer"
73+
},
74+
"name": {
75+
"type": "string"
76+
},
77+
"likes_count": {
78+
"type": "integer"
79+
},
80+
"role": {
81+
"type": "array"
82+
},
83+
"type": {
84+
"type": "string"
85+
}
86+
}
87+
}
88+
}
89+
},
90+
"info": {
91+
"description": "This is automatically collected documentation",
92+
"version": "0.0.0",
93+
"title": "Name of Your Application",
94+
"termsOfService": "",
95+
"contact": {
96+
"email": "[email protected]"
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)