Skip to content

Commit

Permalink
Merge pull request #150 from hans-thomas/refactoring-resolveClassMeth…
Browse files Browse the repository at this point in the history
…odDependencies-method

Refactoring resolveClassMethodDependencies method
  • Loading branch information
DenTray authored Dec 23, 2024
2 parents 07aab91 + bc24820 commit 0916881
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 21 deletions.
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
21 changes: 5 additions & 16 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();
$type = $parameter->getType();

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

return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name;
return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->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]"
}
}
}

0 comments on commit 0916881

Please sign in to comment.