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

Commit

Permalink
Make rule class link to rule source
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Feb 15, 2021
1 parent 1353bd6 commit 058952e
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 28 deletions.
16 changes: 12 additions & 4 deletions packages/rule-doc-generator/src/Finder/ClassByTypeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ClassByTypeFinder
{
/**
* @param string[] $directories
* @return string[]
* @return array<string, string>
*/
public function findByType(array $directories, string $type): array
{
Expand All @@ -24,8 +24,14 @@ public function findByType(array $directories, string $type): array

$robotLoader->rebuild();

$workingDirectory = getcwd();

if (substr($workingDirectory, -1, 1) !== DIRECTORY_SEPARATOR) {
$workingDirectory .= DIRECTORY_SEPARATOR;
}

$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 +42,12 @@ public function findByType(array $directories, string $type): array
continue;
}

$desiredClasses[] = $class;
$relativeFile = str_replace($workingDirectory, '', $file);

$desiredClasses[$relativeFile] = $class;
}

sort($desiredClasses);
asort($desiredClasses);

return $desiredClasses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
}

/**
* @param RuleDefinition[] $ruleDefinitions
* @param array<string, RuleDefinition> $ruleDefinitions
* @return string[]
*/
public function print(array $ruleDefinitions, bool $shouldCategorize): array
Expand Down Expand Up @@ -67,16 +67,16 @@ public function print(array $ruleDefinitions, bool $shouldCategorize): array
}

/**
* @param RuleDefinition[] $ruleDefinitions
* @return array<string, RuleDefinition[]>
* @param array<string, RuleDefinition> $ruleDefinitions
* @return array<string, array<string, RuleDefinition>>
*/
private function groupDefinitionsByCategory(array $ruleDefinitions): array
{
$ruleDefinitionsByCategory = [];

foreach ($ruleDefinitions as $ruleDefinition) {
foreach ($ruleDefinitions as $file => $ruleDefinition) {
$category = $this->categoryResolver->resolve($ruleDefinition);
$ruleDefinitionsByCategory[$category][] = $ruleDefinition;
$ruleDefinitionsByCategory[$category][$file] = $ruleDefinition;
}

ksort($ruleDefinitionsByCategory);
Expand All @@ -85,13 +85,13 @@ private function groupDefinitionsByCategory(array $ruleDefinitions): array
}

/**
* @param RuleDefinition[] $ruleDefinitions
* @param array<string, RuleDefinition> $ruleDefinitions
* @param string[] $lines
* @return string[]
*/
private function printRuleDefinitions(array $ruleDefinitions, array $lines, bool $shouldCategorize = false): array
{
foreach ($ruleDefinitions as $ruleDefinition) {
foreach ($ruleDefinitions as $file => $ruleDefinition) {
if ($shouldCategorize) {
$lines[] = '### ' . $ruleDefinition->getRuleShortClass();
} else {
Expand All @@ -104,7 +104,7 @@ private function printRuleDefinitions(array $ruleDefinitions, array $lines, bool
$lines[] = Lines::CONFIGURE_IT;
}

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

$codeSampleLines = $this->codeSamplePrinter->print($ruleDefinition);
$lines = array_merge($lines, $codeSampleLines);
Expand All @@ -113,7 +113,7 @@ private function printRuleDefinitions(array $ruleDefinitions, array $lines, bool
}

/**
* @param array<string, RuleDefinition[]> $ruleDefinitionsByCategory
* @param array<string, array<string, RuleDefinition>> $ruleDefinitionsByCategory
* @return string[]
*/
private function createCategoryMenu(array $ruleDefinitionsByCategory): array
Expand Down
12 changes: 6 additions & 6 deletions packages/rule-doc-generator/src/RuleDefinitionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ final class RuleDefinitionsResolver
{
/**
* @param string[] $classNames
* @return RuleDefinition[]
* @return array<string, RuleDefinition>
*/
public function resolveFromClassNames(array $classNames): array
{
$ruleDefinitions = [];

foreach ($classNames as $className) {
foreach ($classNames as $fileName => $className) {
$reflectionClass = new ReflectionClass($className);
$documentedRule = $reflectionClass->newInstanceWithoutConstructor();
if (! $documentedRule instanceof DocumentedRuleInterface) {
Expand All @@ -28,19 +28,19 @@ public function resolveFromClassNames(array $classNames): array

$ruleDefinition = $documentedRule->getRuleDefinition();
$ruleDefinition->setRuleClass($className);
$ruleDefinitions[] = $ruleDefinition;
$ruleDefinitions[$fileName] = $ruleDefinition;
}

return $this->sortByClassName($ruleDefinitions);
}

/**
* @param RuleDefinition[] $ruleDefinitions
* @return RuleDefinition[]
* @param array<string, RuleDefinition> $ruleDefinitions
* @return array<string, RuleDefinition>
*/
private function sortByClassName(array $ruleDefinitions): array
{
usort(
uasort(
$ruleDefinitions,
function (RuleDefinition $firstRuleDefinition, RuleDefinition $secondRuleDefinition): int {
return $firstRuleDefinition->getRuleShortClass() <=> $secondRuleDefinition->getRuleShortClass();
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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/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`](packages/rule-doc-generator/tests/DirectoryToMarkdownPrinter/Fixture/Rector/Standard/SomeRector.php)

```diff
-before
Expand Down

0 comments on commit 058952e

Please sign in to comment.