generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from sc-bruno/feature/deprecation-extension
Add @deprecated tag support
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\OperationExtensions; | ||
|
||
use Dedoc\Scramble\Extensions\OperationExtension; | ||
use Dedoc\Scramble\Infer\Reflector\ClassReflector; | ||
use Dedoc\Scramble\Support\Generator\Operation; | ||
use Dedoc\Scramble\Support\PhpDoc; | ||
use Dedoc\Scramble\Support\RouteInfo; | ||
use Illuminate\Support\Str; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; | ||
|
||
/** | ||
* Extension to add deprecation notice to the operation description | ||
* Skips if method is not deprecated | ||
* If a whole class is deprecated, all methods are deprecated, only add the description if exists | ||
*/ | ||
class DeprecationExtension extends OperationExtension | ||
{ | ||
public function handle(Operation $operation, RouteInfo $routeInfo) | ||
{ | ||
// Skip if method is not deprecated | ||
if (!$routeInfo->reflectionMethod() || $routeInfo->phpDoc()->getTagsByName('@not-deprecated')) { | ||
return; | ||
} | ||
|
||
$fqdn = $routeInfo->reflectionMethod()->getDeclaringClass()->getName(); | ||
$deprecatedClass = $this->getClassDeprecatedValues($fqdn); | ||
$deprecatedTags = $routeInfo->phpDoc()->getDeprecatedTagValues(); | ||
|
||
// Skip if no deprecations found | ||
if (!$deprecatedClass && !$deprecatedTags) { | ||
return; | ||
} | ||
|
||
$description = Str::of($this->generateDescription($deprecatedClass)); | ||
|
||
if ($description->isNotEmpty()) { | ||
$description = $description->append("\n\n"); | ||
} | ||
|
||
$description = $description->append($this->generateDescription($deprecatedTags)); | ||
|
||
$operation | ||
->description((string) $description) | ||
->deprecated(true); | ||
} | ||
|
||
/** | ||
* @return array<DeprecatedTagValueNode> | ||
*/ | ||
protected function getClassDeprecatedValues(string $fqdn) | ||
{ | ||
$reflector = ClassReflector::make($fqdn); | ||
$classPhpDocString = $reflector->getReflection()->getDocComment(); | ||
|
||
if ($classPhpDocString === false) { | ||
return []; | ||
} | ||
|
||
return PhpDoc::parse($classPhpDocString)->getDeprecatedTagValues(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function generateDescription(array $deprecation) | ||
{ | ||
return implode("\n", array_map(fn($tag) => $tag->description, $deprecation)); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
tests/Support/OperationExtensions/DeprecationExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route as RouteFacade; | ||
|
||
it('deprecated method sets deprecation key', function () { | ||
$openApiDocument = generateForRoute(function () { | ||
return RouteFacade::get('api/test', [Deprecated_ResponseExtensionTest_Controller::class, 'deprecated']); | ||
}); | ||
|
||
expect($openApiDocument['paths']['/test']['get']) | ||
->not()->toHaveKey('description') | ||
->toHaveKey('deprecated', true); | ||
}); | ||
class Deprecated_ResponseExtensionTest_Controller | ||
{ | ||
/** | ||
* @deprecated | ||
*/ | ||
public function deprecated() | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
it('deprecated method sets key and description', function () { | ||
$openApiDocument = generateForRoute(function () { | ||
return RouteFacade::get('api/test', [Deprecated_Description_ResponseExtensionTest_Controller::class, 'deprecated']); | ||
}); | ||
|
||
expect($openApiDocument['paths']['/test']['get']) | ||
->toHaveKey('description', 'Deprecation description') | ||
->toHaveKey('deprecated', true); | ||
}); | ||
|
||
class Deprecated_Description_ResponseExtensionTest_Controller | ||
{ | ||
/** | ||
* @deprecated Deprecation description | ||
* | ||
* @response array{ "test": "test"} | ||
*/ | ||
public function deprecated() | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
it('deprecated class with description sets keys', function () { | ||
$openApiDocument = generateForRoute(function () { | ||
return RouteFacade::get('api/test', [Deprecated_Class_Description_ResponseExtensionTest_Controller::class, 'deprecated']); | ||
}); | ||
|
||
expect($openApiDocument['paths']['/test']['get']) | ||
->toHaveKey('description', 'Class description' . "\n\n" . 'Deprecation description') | ||
->toHaveKey('deprecated', true); | ||
}); | ||
|
||
/** @deprecated Class description */ | ||
class Deprecated_Class_Description_ResponseExtensionTest_Controller | ||
{ | ||
/** | ||
* @deprecated Deprecation description | ||
* | ||
* @response array{ "test": "test"} | ||
*/ | ||
public function deprecated() | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
it('deprecated class without description sets keys', function () { | ||
$openApiDocument = generateForRoute(function () { | ||
return RouteFacade::get('api/test', [Deprecated_Class_ResponseExtensionTest_Controller::class, 'deprecated']); | ||
}); | ||
|
||
expect($openApiDocument['paths']['/test']['get']) | ||
->not()->toHaveKey('description') | ||
->toHaveKey('deprecated', true); | ||
}); | ||
|
||
/** @deprecated */ | ||
class Deprecated_Class_ResponseExtensionTest_Controller | ||
{ | ||
/** | ||
* @response array{ "test": "test"} | ||
*/ | ||
public function deprecated() | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
it('not deprecated ignores the class deprecation', function () { | ||
$openApiDocument = generateForRoute(function () { | ||
return RouteFacade::get('api/test', [Not_Deprecated_Class_ResponseExtensionTest_Controller::class, 'notDeprecated']); | ||
}); | ||
|
||
expect($openApiDocument['paths']['/test']['get']) | ||
->not()->toHaveKey('description') | ||
->not()->toHaveKey('deprecated'); | ||
}); | ||
|
||
/** @deprecated */ | ||
class Not_Deprecated_Class_ResponseExtensionTest_Controller | ||
{ | ||
/** | ||
* @not-deprecated | ||
*/ | ||
public function notDeprecated() | ||
{ | ||
return false; | ||
} | ||
} |