Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

[RuleDocGenerator] Link rule class to source #2956

Merged
merged 4 commits into from
Feb 15, 2021
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
3 changes: 2 additions & 1 deletion packages/rule-doc-generator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"symplify/markdown-diff": "^9.2",
"symplify/package-builder": "^9.2",
"nette/neon": "^3.2",
"symplify/php-config-printer": "^9.2"
"symplify/php-config-printer": "^9.2",
"symplify/smart-file-system": "^9.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/rule-doc-generator/src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$workingDirectory = getcwd();
$paths = (array) $input->getArgument(Option::PATHS);
$shouldCategorize = (bool) $input->getOption(Option::CATEGORIZE);
$markdownFileContent = $this->directoryToMarkdownPrinter->print($paths, $shouldCategorize);
$markdownFileContent = $this->directoryToMarkdownPrinter->print($workingDirectory,$paths, $shouldCategorize);

// dump markdown file
$outputFilePath = (string) $input->getOption(Option::OUTPUT_FILE);
Expand Down
14 changes: 11 additions & 3 deletions packages/rule-doc-generator/src/DirectoryToMarkdownPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\Finder\ClassByTypeFinder;
use Symplify\RuleDocGenerator\Printer\RuleDefinitionsPrinter;
use Symplify\RuleDocGenerator\ValueObject\RuleClassWithFilePath;

/**
* @see \Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\DirectoryToMarkdownPrinterTest
Expand Down Expand Up @@ -49,15 +50,22 @@ public function __construct(
/**
* @param string[] $directories
*/
public function print(array $directories, bool $shouldCategorize = false): string
public function print(string $workingDirectory, array $directories, bool $shouldCategorize = false): string
{
// 1. collect documented rules in provided path
$documentedRuleClasses = $this->classByTypeFinder->findByType($directories, DocumentedRuleInterface::class);
$documentedRuleClasses = $this->classByTypeFinder->findByType($workingDirectory, $directories, DocumentedRuleInterface::class);

$message = sprintf('Found %d documented rule classes', count($documentedRuleClasses));
$this->symfonyStyle->note($message);

$this->symfonyStyle->listing($documentedRuleClasses);
$classes = array_map(
function (RuleClassWithFilePath $rule) {
return $rule->getClass();
},
$documentedRuleClasses
);

$this->symfonyStyle->listing($classes);

// 2. create rule definition collection
$ruleDefinitions = $this->ruleDefinitionsResolver->resolveFromClassNames($documentedRuleClasses);
Expand Down
21 changes: 16 additions & 5 deletions packages/rule-doc-generator/src/Finder/ClassByTypeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

use Nette\Loaders\RobotLoader;
use ReflectionClass;
use Symplify\RuleDocGenerator\ValueObject\RuleClassWithFilePath;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ClassByTypeFinder
{
/**
* @param string $workingDirectory
* @param string[] $directories
* @return string[]
* @return RuleClassWithFilePath[]
*/
public function findByType(array $directories, string $type): array
public function findByType(string $workingDirectory, array $directories, string $type): array
{
$robotLoader = new RobotLoader();
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/robot_loader_temp');
Expand All @@ -25,7 +28,7 @@ public function findByType(array $directories, string $type): array
$robotLoader->rebuild();

$desiredClasses = [];
foreach (array_keys($robotLoader->getIndexedClasses()) as $class) {
foreach ($robotLoader->getIndexedClasses() as $class => $file) {
if (! is_a($class, $type, true)) {
continue;
}
Expand All @@ -36,10 +39,18 @@ public function findByType(array $directories, string $type): array
continue;
}

$desiredClasses[] = $class;
$fileInfo = new SmartFileInfo($file);
$relativeFilePath = $fileInfo->getRelativeFilePathFromDirectory($workingDirectory);

$desiredClasses[] = new RuleClassWithFilePath($class, $relativeFilePath);
}

sort($desiredClasses);
usort(
$desiredClasses,
function (RuleClassWithFilePath $left, RuleClassWithFilePath $right): int {
return $left->getClass() <=> $right->getClass();
}
);

return $desiredClasses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function printRuleDefinitions(array $ruleDefinitions, array $lines, bool
$lines[] = Lines::CONFIGURE_IT;
}

$lines[] = '- class: `' . $ruleDefinition->getRuleClass() . '`';
$lines[] = '- class: [`' . $ruleDefinition->getRuleClass() . '`](' . $ruleDefinition->getRuleFilePath() . ')';

$codeSampleLines = $this->codeSamplePrinter->print($ruleDefinition);
$lines = array_merge($lines, $codeSampleLines);
Expand Down
10 changes: 6 additions & 4 deletions packages/rule-doc-generator/src/RuleDefinitionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@

use ReflectionClass;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleClassWithFilePath;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\SymplifyKernel\Exception\ShouldNotHappenException;

final class RuleDefinitionsResolver
{
/**
* @param string[] $classNames
* @param RuleClassWithFilePath[] $classNames
* @return RuleDefinition[]
*/
public function resolveFromClassNames(array $classNames): array
{
$ruleDefinitions = [];

foreach ($classNames as $className) {
$reflectionClass = new ReflectionClass($className);
foreach ($classNames as $rule) {
$reflectionClass = new ReflectionClass($rule->getClass());
$documentedRule = $reflectionClass->newInstanceWithoutConstructor();
if (! $documentedRule instanceof DocumentedRuleInterface) {
throw new ShouldNotHappenException();
}

$ruleDefinition = $documentedRule->getRuleDefinition();
$ruleDefinition->setRuleClass($className);
$ruleDefinition->setRuleClass($rule->getClass());
$ruleDefinition->setRuleFilePath($rule->getPath());
$ruleDefinitions[] = $ruleDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Symplify\RuleDocGenerator\ValueObject;

final class RuleClassWithFilePath
{
/**
* @var string
*/
private $class;

/**
* @var string
*/
private $path;

public function __construct(string $class, string $path)
{
$this->class = $class;
$this->path = $path;
}

public function getClass() : string
{
return $this->class;
}

public function getPath() : string
{
return $this->path;
}

}
19 changes: 19 additions & 0 deletions packages/rule-doc-generator/src/ValueObject/RuleDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ final class RuleDefinition
*/
private $ruleClass;

/**
* @var string
*/
private $ruleFilePath;

/**
* @var CodeSampleInterface[]
*/
Expand Down Expand Up @@ -62,6 +67,20 @@ public function getRuleClass(): string
return $this->ruleClass;
}

public function setRuleFilePath(string $ruleFilePath): void
{
$this->ruleFilePath = $ruleFilePath;
}

public function getRuleFilePath(): string
{
if ($this->ruleFilePath === null) {
throw new ShouldNotHappenException();
}

return $this->ruleFilePath;
}

public function getRuleShortClass(): string
{
return (string) Strings::after($this->ruleClass, '\\', -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void
*/
public function test(string $directory, string $expectedFile, bool $shouldCategorize = false): void
{
$fileContent = $this->directoryToMarkdownPrinter->print([$directory], $shouldCategorize);
$fileContent = $this->directoryToMarkdownPrinter->print(__DIR__, [$directory], $shouldCategorize);

$expectedFileInfo = new SmartFileInfo($expectedFile);
StaticFixtureUpdater::updateExpectedFixtureContent($fileContent, $expectedFileInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Some description

:wrench: **configure it!**

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPCSFixer\Configurable\SomeConfiguredFixer`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPCSFixer\Configurable\SomeConfiguredFixer`](Fixture/PHPCSFixer/Configurable/SomeConfiguredFixer.php)

```php
<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Some description

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPCSFixer\Standard\SomeFixer`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPCSFixer\Standard\SomeFixer`](Fixture/PHPCSFixer/Standard/SomeFixer.php)

```diff
-bad code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Some description

:wrench: **configure it!**

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPStan\Configurable\SomePHPStanRule`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPStan\Configurable\SomePHPStanRule`](Fixture/PHPStan/Configurable/SomePHPStanRule.php)

```yaml
services:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Some description

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPStan\Standard\SomePHPStanRule`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\PHPStan\Standard\SomePHPStanRule`](Fixture/PHPStan/Standard/SomePHPStanRule.php)

```php
bad code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Some change

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\ComposerJsonAware\ComposerJsonAwareRector`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\ComposerJsonAware\ComposerJsonAwareRector`](Fixture/Rector/ComposerJsonAware/ComposerJsonAwareRector.php)

- with `composer.json`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Some change

:wrench: **configure it!**

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Configurable\ConfigurableRector`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Configurable\ConfigurableRector`](Fixture/Rector/Configurable/ConfigurableRector.php)

```php
<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Some change

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\ExtraFile\ExtraFileRector`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\ExtraFile\ExtraFileRector`](Fixture/Rector/ExtraFile/ExtraFileRector.php)

```diff
-before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Some change

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Standard\SomeRector`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Standard\SomeRector`](Fixture/Rector/Standard/SomeRector.php)

```diff
-before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Some change

- class: `Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Standard\SomeRector`
- class: [`Symplify\RuleDocGenerator\Tests\DirectoryToMarkdownPrinter\Fixture\Rector\Standard\SomeRector`](Fixture/Rector/Standard/SomeRector.php)

```diff
-before
Expand Down