-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPN-1440] New Guardrail Check to ensure documentation is present and…
… report on deprecated web APIs
- Loading branch information
1 parent
b704af0
commit f4798fc
Showing
7 changed files
with
162 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,66 @@ | ||
<?php | ||
|
||
namespace BambooHR\Guardrail\Checks; | ||
|
||
use BambooHR\Guardrail\Metrics\Metric; | ||
use BambooHR\Guardrail\Metrics\MetricOutputInterface; | ||
use BambooHR\Guardrail\Scope; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
|
||
class WebApiDocumentationCheck extends BaseCheck { | ||
function __construct($index, $output, private readonly MetricOutputInterface $metricOutput) { | ||
parent::__construct($index, $output); | ||
} | ||
|
||
/** | ||
* getCheckNodeTypes | ||
* | ||
* @return string[] | ||
*/ | ||
function getCheckNodeTypes() { | ||
return [Node\Stmt\ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param string $fileName The name of the file we are parsing | ||
* @param Node $node Instance of the Node | ||
* @param ClassLike|null $inside Instance of the ClassLike (the class we are parsing) [optional] | ||
* @param Scope|null $scope Instance of the Scope (all variables in the current state) [optional] | ||
* | ||
* @return void | ||
*/ | ||
public function run($fileName, Node $node, ClassLike $inside = null, Scope $scope = null) { | ||
if ($node instanceof Node\Stmt\ClassMethod && $node->isPublic()) { | ||
foreach ($node->attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attribute) { | ||
$attributeName = $attribute->name->toString(); | ||
if (str_starts_with($attributeName, 'OpenApi\Attributes')) { | ||
foreach ($attribute->args as $arg) { | ||
if ($arg->name->name === 'deprecated' && $arg->value->name->toString() == 'true') { | ||
$this->metricOutput->emitMetric(new Metric( | ||
$fileName, | ||
$node->getLine(), | ||
ErrorConstants::TYPE_METRICS_DEPRECATED_FUNCTIONS, | ||
[] | ||
)); | ||
break; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
} | ||
} | ||
|
||
$className = $inside->namespacedName->toString(); | ||
$this->emitErrorOnLine( | ||
$fileName, | ||
$node->getLine(), | ||
ErrorConstants::TYPE_WEB_API_DOCUMENTATION_CHECK, | ||
"All public controller methods should be associated with a route and must have | ||
documentation through an OpenAPI Attribute. Method: {$node->name->name}, Class: $className" | ||
); | ||
} | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
tests/units/Checks/TestData/TestWebApiDocumentationCheck.1.inc
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,29 @@ | ||
<?php | ||
use OpenApi\Attributes as OA; | ||
|
||
|
||
class MyController { | ||
/** | ||
* @return bool | ||
*/ | ||
#[\Onsen\SecurityAudit\Sensitivity\Low] | ||
#[OA\Get(path: "/test")] | ||
public function hasAttribute() { | ||
return false; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function doesNotHaveAttribute() { | ||
return 123; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
#[\Onsen\SecurityAudit\Sensitivity\Low] | ||
public function hasFakeAttribute() { | ||
return 456; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/units/Checks/TestData/TestWebApiDocumentationCheck.2.inc
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,17 @@ | ||
<?php | ||
use OpenApi\Attributes as OA; | ||
|
||
class MyController { | ||
function undefinedVisibilityMethod() { | ||
return false; | ||
} | ||
public function publicMethod() { | ||
return false; | ||
} | ||
protected function protectedMethod() { | ||
return false; | ||
} | ||
private function privateMethod() { | ||
return false; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/units/Checks/TestData/TestWebApiDocumentationCheck.3.inc
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,18 @@ | ||
<?php | ||
use OpenApi\Attributes as OA; | ||
|
||
|
||
class MyController { | ||
/** | ||
* @return bool | ||
*/ | ||
#[OA\Get(path: "/test", deprecated: true)] | ||
public function hasDeprecatedAttribute() { | ||
return false; | ||
} | ||
|
||
#[OA\Get(path: "/test")] | ||
public function doesNotHaveDeprecatedAttribute() { | ||
return false; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace BambooHR\Guardrail\Tests\units\Checks; | ||
|
||
use BambooHR\Guardrail\Checks\ErrorConstants; | ||
use BambooHR\Guardrail\Tests\TestSuiteSetup; | ||
|
||
class TestWebApiDocumentationCheck extends TestSuiteSetup { | ||
/** | ||
* testApiAttributeIsPresent | ||
* | ||
* @return void | ||
*/ | ||
public function testApiAttributeIsPresent() { | ||
$this->assertEquals(2, $this->runAnalyzerOnFile('.1.inc', ErrorConstants::TYPE_WEB_API_DOCUMENTATION_CHECK,), ""); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testOnlyErrorsOnPublicMethods() { | ||
$this->assertEquals(2, $this->runAnalyzerOnFile('.2.inc', ErrorConstants::TYPE_WEB_API_DOCUMENTATION_CHECK,), ""); | ||
} | ||
|
||
public function testMethodWithDeprecatedAttribute() { | ||
$output = $this->getOutputFromAnalyzer('.3.inc', ErrorConstants::TYPE_METRICS_DEPRECATED_FUNCTIONS); | ||
$this->assertEquals(1, $this->getMetricCountByName($output, ErrorConstants::TYPE_METRICS_DEPRECATED_FUNCTIONS)); | ||
} | ||
} |