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

Require request validation formatter to extend controller #346

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Commands/FormatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function formatFile(InputInterface $input, OutputInterface $output, $fil
if (! empty($only = $input->getOption('only'))) {
$formatters = array_filter($this->getAllFormatters($file), function ($formatter) use ($only) {
foreach ($only as $filter) {
if (false !== strpos($formatter, $filter)) {
if (strpos($formatter, $filter) !== false) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function lintFile(InputInterface $input, OutputInterface $output, $file)
if (! empty($only = $input->getOption('only'))) {
$linters = array_filter($this->getAllLinters($file), function ($linter) use ($only) {
foreach ($only as $filter) {
if (false !== strpos($linter, $filter)) {
if (strpos($linter, $filter) !== false) {
return true;
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/Formatters/RequestValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,28 @@ private function visitor(): NodeVisitorAbstract
{
return new class extends NodeVisitorAbstract
{
private bool $extendsController = false;

public function beforeTraverse(array $nodes)
{
$this->extendsController = false;

return null;
}

public function enterNode(Node $node): Node|int|null
{
if ($node instanceof Node\Stmt\Class_
&& ! empty($node->extends)
&& $node->extends->toString() === 'Controller'
) {
$this->extendsController = true;
}

if (! $this->extendsController) {
return null;
}

if (! $node instanceof Node\Expr\MethodCall) {
return null;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Formatting/Formatters/RequestValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,26 @@ public function store()

$this->assertSame($file, $formatted);
}

/** @test */
public function it_ignores_classes_that_do_not_extend_controller()
{
$file = <<<'file'
<?php

namespace App;

class ControllerA extends NotController
{
public function store()
{
$this->validate(['name' => 'required'], ['name.required' => 'Name is required']);
}
}
file;

$formatted = (new TFormat)->format(new RequestValidation($file));

$this->assertSame($file, $formatted);
}
}
Loading