-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sniff to disallow repository usage in controller (#640)
- Loading branch information
1 parent
9b0e95a
commit e3e98d9
Showing
7 changed files
with
247 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
37 changes: 37 additions & 0 deletions
37
src/Standards/RepositoryPattern/Sniffs/Controllers/DisallowRepositoryUsageSniff.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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Standards\RepositoryPattern\Sniffs\Controllers; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
class DisallowRepositoryUsageSniff implements Sniff | ||
{ | ||
public function register(): array | ||
{ | ||
return [T_USE]; | ||
} | ||
|
||
public function process(File $phpcsFile, $stackPtr): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$fileName = $phpcsFile->getFilename(); | ||
|
||
if (false === str_contains($fileName, 'src/Controller')) { | ||
return; | ||
} | ||
|
||
$endPtr = $phpcsFile->findNext([T_SEMICOLON, T_AS], $stackPtr); | ||
$className = ''; | ||
for ($i = $stackPtr + 1; $i < $endPtr; $i++) { | ||
$className .= $tokens[$i]['content']; | ||
} | ||
|
||
if (true === str_contains($className, 'Repository')) { | ||
$error = 'Controllers must not use repositories directly; use services instead.'; | ||
$phpcsFile->addError($error, $stackPtr, 'RepositoryUsageDetected'); | ||
} | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="RepositoryPattern"> | ||
<description>A custom coding standard for repository pattern.</description> | ||
</ruleset> |
118 changes: 118 additions & 0 deletions
118
...ional/Standards/RepositoryPattern/Sniffs/Controllers/DisallowRepositoryUsageSniffTest.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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Functional\Standards\RepositoryPattern\Sniffs\Controllers; | ||
|
||
use PHP_CodeSniffer\Config; | ||
use PHP_CodeSniffer\Files\LocalFile; | ||
use PHP_CodeSniffer\Ruleset; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DisallowRepositoryUsageSniffTest extends TestCase | ||
{ | ||
private string $tempDir; | ||
private string $tempFilePath; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->tempDir = sys_get_temp_dir().'/src/Controller'; | ||
if (!is_dir($this->tempDir)) { | ||
mkdir($this->tempDir, 0o777, true); | ||
} | ||
|
||
$this->tempFilePath = $this->tempDir.'/UserController.php'; | ||
$this->createTemporaryFile($this->tempFilePath); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
if (file_exists($this->tempFilePath)) { | ||
unlink($this->tempFilePath); | ||
} | ||
if (is_dir($this->tempDir)) { | ||
rmdir($this->tempDir); | ||
} | ||
if (is_dir(dirname($this->tempDir))) { | ||
rmdir(dirname($this->tempDir)); | ||
} | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testRepositoryUsageInControllerIsDetected(): void | ||
{ | ||
$config = new Config(); | ||
|
||
$rulesetPath = dirname(__DIR__, 6).'/src/Standards/RepositoryPattern/ruleset.xml'; | ||
$config->standards = [$rulesetPath]; | ||
|
||
$ruleset = new Ruleset($config); | ||
|
||
$file = new LocalFile($this->tempFilePath, $ruleset, $config); | ||
$file->process(); | ||
|
||
$errors = $file->getErrors(); | ||
$this->assertNotEmpty($errors, 'Expected an error to be detected.'); | ||
|
||
$foundError = $this->containsErrorMessage( | ||
$errors | ||
); | ||
|
||
$this->assertTrue($foundError, 'Expected error message not found.'); | ||
} | ||
|
||
// phpcs:disable RepositoryPattern.Controllers.DisallowRepositoryUsage | ||
private function createTemporaryFile(string $path): void | ||
{ | ||
$content = <<<'PHP' | ||
<?php | ||
declare(strict_types=1); | ||
namespace App\Controller; | ||
use App\Repository\UserRepository; | ||
class UserController | ||
{ | ||
private $userRepository; | ||
public function __construct(UserRepository $userRepository) | ||
{ | ||
$this->userRepository = $userRepository; | ||
} | ||
public function index() | ||
{ | ||
$users = $this->userRepository->findAll(); | ||
} | ||
} | ||
PHP; | ||
|
||
$tempFile = fopen($path, 'w'); | ||
if (false === $tempFile) { | ||
$this->fail('Failed to create temporary file.'); | ||
} | ||
|
||
fwrite($tempFile, $content); | ||
fclose($tempFile); | ||
} | ||
// phpcs:enable RepositoryPattern.Controllers.DisallowRepositoryUsage | ||
|
||
private function containsErrorMessage(array $errors): bool | ||
{ | ||
return array_any( | ||
$errors, | ||
fn ($lineErrors) => array_any( | ||
$lineErrors, | ||
fn ($errorMessages) => array_any( | ||
$errorMessages, | ||
fn ($errorMessage) => str_contains($errorMessage['message'], 'Controllers must not use repositories directly') | ||
) | ||
) | ||
); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
.../Unit/Standards/RepositoryPattern/Sniffs/Controllers/DisallowRepositoryUsageSniffTest.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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Standards\RepositoryPattern\Sniffs\Controllers; | ||
|
||
use App\Standards\RepositoryPattern\Sniffs\Controllers\DisallowRepositoryUsageSniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DisallowRepositoryUsageSniffTest extends TestCase | ||
{ | ||
public function testRegisterReturnsCorrectTokens(): void | ||
{ | ||
$sniff = new DisallowRepositoryUsageSniff(); | ||
$this->assertSame([T_USE], $sniff->register()); | ||
} | ||
|
||
public function testSniffDetectsRepositoryUsage(): void | ||
{ | ||
$sniff = new DisallowRepositoryUsageSniff(); | ||
$fileMock = $this->createMock(File::class); | ||
|
||
$fileMock->method('getFilename') | ||
->willReturn('/src/Controller/Api/SomeController.php'); | ||
|
||
$tokens = [ | ||
['content' => 'use'], | ||
['content' => ' App\\Repository\\SomeRepository'], | ||
['content' => ';'], | ||
]; | ||
|
||
$fileMock->method('getTokens')->willReturn($tokens); | ||
$fileMock->method('findNext')->willReturn(2); | ||
|
||
$fileMock->expects($this->once()) | ||
->method('addError') | ||
->with( | ||
'Controllers must not use repositories directly; use services instead.', | ||
$this->anything(), | ||
'RepositoryUsageDetected' | ||
); | ||
|
||
$sniff->process($fileMock, 0); | ||
} | ||
|
||
public function testSniffIgnoresNonControllerFiles(): void | ||
{ | ||
$sniff = new DisallowRepositoryUsageSniff(); | ||
$fileMock = $this->createMock(File::class); | ||
|
||
$fileMock->method('getFilename') | ||
->willReturn('/src/Service/SomeService.php'); | ||
|
||
$fileMock->expects($this->never())->method('addError'); | ||
|
||
$sniff->process($fileMock, 0); | ||
} | ||
|
||
public function testSniffIgnoresNonRepositoryUsage(): void | ||
{ | ||
$sniff = new DisallowRepositoryUsageSniff(); | ||
$fileMock = $this->createMock(File::class); | ||
|
||
$fileMock->method('getFilename') | ||
->willReturn('/src/Controller/Api/SomeController.php'); | ||
|
||
$tokens = [ | ||
['content' => 'use'], | ||
['content' => ' App\\Service\\SomeService'], | ||
['content' => ';'], | ||
]; | ||
|
||
$fileMock->method('getTokens')->willReturn($tokens); | ||
$fileMock->method('findNext')->willReturn(2); | ||
|
||
$fileMock->expects($this->never())->method('addError'); | ||
|
||
$sniff->process($fileMock, 0); | ||
} | ||
} |
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