Skip to content

Commit

Permalink
New rule : Ensure :class: :method: :namespace: starts with prefix (#1672
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MrYamous authored Mar 11, 2024
1 parent 89beb92 commit 93962eb
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Rule/EnsureGithubDirectiveStartWithPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/**
* This file is part of DOCtor-RST.
*
* (c) Oskar Stark <oskarstark@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Rule;

use App\Value\Lines;
use App\Value\NullViolation;
use App\Value\Violation;
use App\Value\ViolationInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EnsureGithubDirectiveStartWithPrefix extends AbstractRule implements Configurable, LineContentRule
{
private string $prefix;

public function configureOptions(OptionsResolver $resolver): OptionsResolver
{
$resolver
->setRequired('prefix')
->setAllowedTypes('prefix', 'string');

return $resolver;
}

public function setOptions(array $options): void
{
$resolver = $this->configureOptions(new OptionsResolver());

$resolvedOptions = $resolver->resolve($options);

$this->prefix = $resolvedOptions['prefix'];
}

public function check(Lines $lines, int $number, string $filename): ViolationInterface
{
$lines->seek($number);
$line = $lines->current();

if ($line->clean()->match('/:(method|class|namespace):`.*`/') &&
!($line->clean()->match('/:(method|class|namespace):`'.$this->prefix.'\\\\.*`/'))) {

$message = sprintf(
'Please only use "%s" base namespace with Github directive',
$this->prefix,
);

return Violation::from(
$message,
$filename,
$number + 1,
$line,
);
}

return NullViolation::create();
}
}
64 changes: 64 additions & 0 deletions tests/Rule/EnsureGithubDirectiveStartWithPrefixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* This file is part of DOCtor-RST.
*
* (c) Oskar Stark <oskarstark@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Tests\Rule;

use App\Rule\EnsureGithubDirectiveStartWithPrefix;
use App\Tests\RstSample;
use App\Tests\UnitTestCase;
use App\Value\NullViolation;
use App\Value\Violation;
use App\Value\ViolationInterface;

final class EnsureGithubDirectiveStartWithPrefixTest extends UnitTestCase
{
/**
* @test
*
* @dataProvider checkProvider
*/
public function check(ViolationInterface $expected, string $prefix, RstSample $sample): void
{
$rule = (new EnsureGithubDirectiveStartWithPrefix());
$rule->setOptions(['prefix' => $prefix]);

self::assertEquals($expected, $rule->check($sample->lines, $sample->lineNumber, 'filename'));
}

/**
* @return \Generator<array{0: ViolationInterface, 1: string, 2: RstSample}>
*/
public static function checkProvider(): iterable
{
yield [
NullViolation::create(),
'Symfony',
new RstSample('Using :class:`Symfony\\Component\\Cache\\Adapter\\PdoAdapter` is deprecated'),
];
yield [
NullViolation::create(),
'Psr',
new RstSample('Implements the interface :class:`Psr\\Cache\\CacheItemPoolInterface`'),
];
yield [
Violation::from(
'Please only use "Symfony" base namespace with Github directive',
'filename',
1,
'Implements the interface :class:`Psr\\Cache\\CacheItemPoolInterface`',
),
'Symfony',
new RstSample('Implements the interface :class:`Psr\\Cache\\CacheItemPoolInterface`'),
];
}
}

0 comments on commit 93962eb

Please sign in to comment.