Skip to content

Commit

Permalink
Merge branch '2.15' into 2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Nov 12, 2020
2 parents cd92951 + a618eee commit e192e69
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
2 changes: 2 additions & 0 deletions dev-tools/doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* with this source code in the file LICENSE.
*/

error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);

require __DIR__.'/../vendor/autoload.php';

use PhpCsFixer\Console\Command\DocumentationCommand;
Expand Down
21 changes: 17 additions & 4 deletions src/Console/Command/DocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->generateFixersDocs($fixers);
$this->generateRuleSetsDocs($fixers);

$output->writeln('Docs updated.');

return 0;
}

private function generateFixersDocs(array $fixers)
{
$filesystem = new Filesystem();

/** @var SplFileInfo $file */
foreach ((new Finder())->files()->in($this->generator->getFixersDocumentationDirectoryPath()) as $file) {
$filesystem->remove($file->getPathname());
}
// Array of existing fixer docs.
// We first override existing files, and then we will delete files that are no longer needed.
// We cannot remove all files first, as generation of docs is re-using existing docs to extract code-samples for
// VersionSpecificCodeSample under incompatible PHP version.
$docForFixerRelativePaths = [];

foreach ($fixers as $fixer) {
$docForFixerRelativePaths[] = $this->generator->getFixerDocumentationFileRelativePath($fixer);
$filesystem->dumpFile(
$this->generator->getFixerDocumentationFilePath($fixer),
$this->generator->generateFixerDocumentation($fixer)
);
}

/** @var SplFileInfo $file */
foreach (
(new Finder())->files()
->in($this->generator->getFixersDocumentationDirectoryPath())
->notPath($docForFixerRelativePaths) as $file
) {
$filesystem->remove($file->getPathname());
}

$index = $this->generator->getFixersDocumentationIndexFilePath();

if (false === @file_put_contents($index, $this->generator->generateFixersDocumentationIndex($fixers))) {
Expand Down
36 changes: 27 additions & 9 deletions src/Documentation/DocumentationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ public function generateFixersDocumentationIndex(array $fixers)
$attributes = '';
}

$path = Preg::replace(
'#^'.preg_quote($this->getFixersDocumentationDirectoryPath(), '#').'/#',
'./',
$this->getFixerDocumentationFilePath($fixer)
);
$path = './'.$this->getFixerDocumentationFileRelativePath($fixer);

$documentation .= <<<RST
Expand All @@ -159,6 +155,18 @@ function (array $matches) {
).'.rst';
}

/**
* @return string
*/
public function getFixerDocumentationFileRelativePath(FixerInterface $fixer)
{
return Preg::replace(
'#^'.preg_quote($this->getFixersDocumentationDirectoryPath(), '#').'/#',
'',
$this->getFixerDocumentationFilePath($fixer)
);
}

/**
* @return string
*/
Expand Down Expand Up @@ -304,7 +312,7 @@ public function generateFixerDocumentation(FixerInterface $fixer)
}
}

$doc .= "\n".$this->generateSampleDiff($fixer, $sample, $index, $name);
$doc .= "\n".$this->generateSampleDiff($fixer, $sample, $index + 1, $name);
}
}

Expand Down Expand Up @@ -450,18 +458,28 @@ public function generateRuleSetsDocumentationIndex(array $setDefinitions)
}

/**
* @param int $sampleIndex
* @param int $sampleNumber
* @param string $ruleName
*
* @return string
*/
private function generateSampleDiff(FixerInterface $fixer, CodeSampleInterface $sample, $sampleIndex, $ruleName)
private function generateSampleDiff(FixerInterface $fixer, CodeSampleInterface $sample, $sampleNumber, $ruleName)
{
if ($sample instanceof VersionSpecificCodeSampleInterface && !$sample->isSuitableFor(\PHP_VERSION_ID)) {
$existingFile = @file_get_contents($this->getFixerDocumentationFilePath($fixer));

if (false !== $existingFile) {
Preg::match("/\\RExample #{$sampleNumber}\\R.+?(?<diff>\\R\\.\\. code-block:: diff\\R\\R.*?)\\R(?:\\R\\S|$)/s", $existingFile, $matches);

if (isset($matches['diff'])) {
return $matches['diff'];
}
}

$error = <<<RST
.. error::
Cannot generate diff for code sample #{$sampleIndex} of rule {$ruleName}:
Cannot generate diff for code sample #{$sampleNumber} of rule {$ruleName}:
the sample is not suitable for current version of PHP (%s).
RST;

Expand Down

0 comments on commit e192e69

Please sign in to comment.