Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring resolveClassMethodDependencies method #150

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ public function getConcreteRequest()
$class = $explodedController[0];
$method = $explodedController[1];

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

$parameters = $this->resolveClassMethodDependencies(
$route->parametersWithoutNulls(),
$instance,
app($class),
$method
);

Expand Down
19 changes: 4 additions & 15 deletions src/Traits/GetDependenciesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,22 @@

trait GetDependenciesTrait
{
protected function resolveClassMethodDependencies(array $parameters, $instance, $method)
{
if (!method_exists($instance, $method)) {
return $parameters;
}

return $this->getDependencies(
new ReflectionMethod($instance, $method)
);
}

public function getDependencies(ReflectionFunctionAbstract $reflector)
public function resolveClassMethodDependencies(object $instance, string $method): array
{
return array_map(function ($parameter) {
return $this->transformDependency($parameter);
}, $reflector->getParameters());
}, (new ReflectionMethod($instance, $method))->getParameters());
}

protected function transformDependency(ReflectionParameter $parameter)
{
$class = $parameter->getClass();
$class = $parameter->getType();

if (empty($class)) {
return null;
}

return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name;
return interface_exists($class->getName()) ? $this->getClassByInterface($class->getName()) : $class->getName();
}

protected function getClassByInterface($interfaceName)
Expand Down
2 changes: 1 addition & 1 deletion tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public function testAddDataPostRequestWithObjectParams()

public function testAddDataWithNotExistsMethodOnController()
{
$this->mockDriverGetTmpData($this->getJsonFixture('tmp_data_get_user_request'));
$this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_get_user_request_without_request_class'));

$service = app(SwaggerService::class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"openapi": "3.1.0",
"servers": [
{
"url": "http:\/\/localhost"
}
],
"paths": {
"/users/{id}/assign-role/{role-id}": {
"get": {
"tags": [
"users"
],
"consumes": [],
"produces": [
"application/json"
],
"parameters": [
{
"in": "path",
"name": "id",
"description": "",
"required": true,
"schema": {
"type": "string"
}
},
{
"in": "path",
"name": "role-id",
"description": "",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/getUsers{id}assignRole{roleId}200ResponseObject",
"type": "object"
},
"example": {
"id": 2,
"name": "first_client",
"likes_count": 23,
"role": {
"id": 2,
"name": "client"
},
"type": "reader"
}
}
}
}
},
"security": [],
"description": ""
}
}
},
"components": {
"schemas": {
"getUsers{id}assignRole{roleId}200ResponseObject": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"likes_count": {
"type": "integer"
},
"role": {
"type": "array"
},
"type": {
"type": "string"
}
}
}
}
},
"info": {
"description": "This is automatically collected documentation",
"version": "0.0.0",
"title": "Name of Your Application",
"termsOfService": "",
"contact": {
"email": "[email protected]"
}
}
}
Loading