Skip to content

Commit

Permalink
DX: Narrow docblock types in Runner and Report (#6465)
Browse files Browse the repository at this point in the history
* narrow docblock types in runner

* narrow docblock types in report

* simplify
  • Loading branch information
jrmajor committed Jul 19, 2022
1 parent fab645b commit b674087
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 53 deletions.
20 changes: 2 additions & 18 deletions phpstan.neon
Expand Up @@ -21,28 +21,12 @@ parameters:

# baseline, should only shrink
-
message: '#^Method .+ has parameter .+ with no value type specified in iterable type array\.$#'
message: '#^.+no value type specified in iterable type.+\.$#'
path: *
count: 363
-
message: '#^Method .+ has parameter .+ with no value type specified in iterable type iterable\.$#'
path: *
count: 4
-
message: '#^Method .+ return type has no value type specified in iterable type array\.$#'
path: *
count: 153
-
message: '#^Method .+ return type has no value type specified in iterable type iterable\.$#'
path: *
count: 15
count: 561
-
message: '#^Property .+ has no type specified\.$#'
path: *
count: 7
-
message: '#^Property .+ type has no value type specified in iterable type array\.$#'
path: *
count: 37
tipsOfTheDay: false
tmpDir: dev-tools/phpstan/cache
2 changes: 1 addition & 1 deletion src/Console/Report/FixReport/JsonReporter.php
Expand Up @@ -45,7 +45,7 @@ public function generate(ReportSummary $reportSummary): string
$jsonFile['appliedFixers'] = $fixResult['appliedFixers'];
}

if (!empty($fixResult['diff'])) {
if ('' !== $fixResult['diff']) {
$jsonFile['diff'] = $fixResult['diff'];
}

Expand Down
5 changes: 4 additions & 1 deletion src/Console/Report/FixReport/JunitReporter.php
Expand Up @@ -102,6 +102,9 @@ private function createFailedTestCases(\DOMDocument $dom, \DOMElement $testsuite
$testsuite->setAttribute('errors', '0');
}

/**
* @param array{appliedFixers: list<string>, diff: string} $fixResult
*/
private function createFailedTestCase(\DOMDocument $dom, string $file, array $fixResult, bool $shouldAddAppliedFixers): \DOMElement
{
$appliedFixersCount = \count($fixResult['appliedFixers']);
Expand All @@ -127,7 +130,7 @@ private function createFailedTestCase(\DOMDocument $dom, string $file, array $fi
$failureContent = "Wrong code style\n";
}

if (!empty($fixResult['diff'])) {
if ('' !== $fixResult['diff']) {
$failureContent .= "\nDiff:\n---------------\n\n".$fixResult['diff'];
}

Expand Down
11 changes: 9 additions & 2 deletions src/Console/Report/FixReport/ReportSummary.php
Expand Up @@ -21,6 +21,9 @@
*/
final class ReportSummary
{
/**
* @var array<string, array{appliedFixers: list<string>, diff: string}>
*/
private array $changed;

private int $time;
Expand All @@ -34,8 +37,9 @@ final class ReportSummary
private bool $isDecoratedOutput;

/**
* @param int $time duration in milliseconds
* @param int $memory memory usage in bytes
* @param array<string, array{appliedFixers: list<string>, diff: string}> $changed
* @param int $time duration in milliseconds
* @param int $memory memory usage in bytes
*/
public function __construct(
array $changed,
Expand Down Expand Up @@ -63,6 +67,9 @@ public function isDryRun(): bool
return $this->isDryRun;
}

/**
* @return array<string, array{appliedFixers: list<string>, diff: string}>
*/
public function getChanged(): array
{
return $this->changed;
Expand Down
8 changes: 3 additions & 5 deletions src/Console/Report/FixReport/ReporterFactory.php
Expand Up @@ -15,7 +15,6 @@
namespace PhpCsFixer\Console\Report\FixReport;

use Symfony\Component\Finder\Finder as SymfonyFinder;
use Symfony\Component\Finder\SplFileInfo;

/**
* @author Boris Gorbylev <ekho@ekho.name>
Expand All @@ -25,19 +24,18 @@
final class ReporterFactory
{
/**
* @var ReporterInterface[]
* @var array<string, ReporterInterface>
*/
private array $reporters = [];

public function registerBuiltInReporters(): self
{
/** @var null|string[] $builtInReporters */
/** @var null|list<string> $builtInReporters */
static $builtInReporters;

if (null === $builtInReporters) {
$builtInReporters = [];

/** @var SplFileInfo $file */
foreach (SymfonyFinder::create()->files()->name('*Reporter.php')->in(__DIR__) as $file) {
$relativeNamespace = $file->getRelativePath();
$builtInReporters[] = sprintf(
Expand Down Expand Up @@ -73,7 +71,7 @@ public function registerReporter(ReporterInterface $reporter): self
}

/**
* @return string[]
* @return list<string>
*/
public function getFormats(): array
{
Expand Down
20 changes: 13 additions & 7 deletions src/Console/Report/FixReport/TextReporter.php
Expand Up @@ -44,27 +44,33 @@ public function generate(ReportSummary $reportSummary): string
$output .= sprintf('%4d) %s', $i, $file);

if ($reportSummary->shouldAddAppliedFixers()) {
$output .= $this->getAppliedFixers($reportSummary->isDecoratedOutput(), $fixResult);
$output .= $this->getAppliedFixers(
$reportSummary->isDecoratedOutput(),
$fixResult['appliedFixers'],
);
}

$output .= $this->getDiff($reportSummary->isDecoratedOutput(), $fixResult);
$output .= $this->getDiff($reportSummary->isDecoratedOutput(), $fixResult['diff']);
$output .= PHP_EOL;
}

return $output.$this->getFooter($reportSummary->getTime(), $reportSummary->getMemory(), $reportSummary->isDryRun());
}

private function getAppliedFixers(bool $isDecoratedOutput, array $fixResult): string
/**
* @param list<string> $appliedFixers
*/
private function getAppliedFixers(bool $isDecoratedOutput, array $appliedFixers): string
{
return sprintf(
$isDecoratedOutput ? ' (<comment>%s</comment>)' : ' (%s)',
implode(', ', $fixResult['appliedFixers'])
implode(', ', $appliedFixers)
);
}

private function getDiff(bool $isDecoratedOutput, array $fixResult): string
private function getDiff(bool $isDecoratedOutput, string $diff): string
{
if (empty($fixResult['diff'])) {
if ('' === $diff) {
return '';
}

Expand All @@ -74,7 +80,7 @@ private function getDiff(bool $isDecoratedOutput, array $fixResult): string
PHP_EOL
));

return PHP_EOL.$diffFormatter->format($fixResult['diff']).PHP_EOL;
return PHP_EOL.$diffFormatter->format($diff).PHP_EOL;
}

private function getFooter(int $time, int $memory, bool $isDryRun): string
Expand Down
19 changes: 12 additions & 7 deletions src/Console/Report/FixReport/XmlReporter.php
Expand Up @@ -56,11 +56,13 @@ public function generate(ReportSummary $reportSummary): string
$filesXML->appendChild($fileXML);

if ($reportSummary->shouldAddAppliedFixers()) {
$fileXML->appendChild($this->createAppliedFixersElement($dom, $fixResult));
$fileXML->appendChild(
$this->createAppliedFixersElement($dom, $fixResult['appliedFixers']),
);
}

if (!empty($fixResult['diff'])) {
$fileXML->appendChild($this->createDiffElement($dom, $fixResult));
if ('' !== $fixResult['diff']) {
$fileXML->appendChild($this->createDiffElement($dom, $fixResult['diff']));
}
}

Expand All @@ -77,11 +79,14 @@ public function generate(ReportSummary $reportSummary): string
return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($dom->saveXML()) : $dom->saveXML();
}

private function createAppliedFixersElement(\DOMDocument $dom, array $fixResult): \DOMElement
/**
* @param list<string> $appliedFixers
*/
private function createAppliedFixersElement(\DOMDocument $dom, array $appliedFixers): \DOMElement
{
$appliedFixersXML = $dom->createElement('applied_fixers');

foreach ($fixResult['appliedFixers'] as $appliedFixer) {
foreach ($appliedFixers as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
Expand All @@ -90,10 +95,10 @@ private function createAppliedFixersElement(\DOMDocument $dom, array $fixResult)
return $appliedFixersXML;
}

private function createDiffElement(\DOMDocument $dom, array $fixResult): \DOMElement
private function createDiffElement(\DOMDocument $dom, string $diff): \DOMElement
{
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$diffXML->appendChild($dom->createCDATASection($diff));

return $diffXML;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Report/ListSetsReport/ReportSummary.php
Expand Up @@ -24,20 +24,20 @@
final class ReportSummary
{
/**
* @var RuleSetDescriptionInterface[]
* @var list<RuleSetDescriptionInterface>
*/
private array $sets;

/**
* @param RuleSetDescriptionInterface[] $sets
* @param list<RuleSetDescriptionInterface> $sets
*/
public function __construct(array $sets)
{
$this->sets = $sets;
}

/**
* @return RuleSetDescriptionInterface[]
* @return list<RuleSetDescriptionInterface>
*/
public function getSets(): array
{
Expand Down
8 changes: 3 additions & 5 deletions src/Console/Report/ListSetsReport/ReporterFactory.php
Expand Up @@ -15,7 +15,6 @@
namespace PhpCsFixer\Console\Report\ListSetsReport;

use Symfony\Component\Finder\Finder as SymfonyFinder;
use Symfony\Component\Finder\SplFileInfo;

/**
* @author Boris Gorbylev <ekho@ekho.name>
Expand All @@ -25,19 +24,18 @@
final class ReporterFactory
{
/**
* @var ReporterInterface[]
* @var array<string, ReporterInterface>
*/
private array $reporters = [];

public function registerBuiltInReporters(): self
{
/** @var null|string[] $builtInReporters */
/** @var null|list<string> $builtInReporters */
static $builtInReporters;

if (null === $builtInReporters) {
$builtInReporters = [];

/** @var SplFileInfo $file */
foreach (SymfonyFinder::create()->files()->name('*Reporter.php')->in(__DIR__) as $file) {
$relativeNamespace = $file->getRelativePath();
$builtInReporters[] = sprintf(
Expand Down Expand Up @@ -70,7 +68,7 @@ public function registerReporter(ReporterInterface $reporter): self
}

/**
* @return string[]
* @return list<string>
*/
public function getFormats(): array
{
Expand Down
5 changes: 5 additions & 0 deletions src/Runner/FileCachingLintingIterator.php
Expand Up @@ -21,6 +21,8 @@
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @extends \CachingIterator<mixed, \SplFileInfo, \Iterator<mixed, \SplFileInfo>>
*/
final class FileCachingLintingIterator extends \CachingIterator
{
Expand All @@ -36,6 +38,9 @@ final class FileCachingLintingIterator extends \CachingIterator
*/
private $nextResult;

/**
* @param \Iterator<mixed, \SplFileInfo> $iterator
*/
public function __construct(\Iterator $iterator, LinterInterface $linter)
{
parent::__construct($iterator);
Expand Down
4 changes: 3 additions & 1 deletion src/Runner/FileFilterIterator.php
Expand Up @@ -24,6 +24,8 @@
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @extends \FilterIterator<mixed, \SplFileInfo, \Iterator<mixed, \SplFileInfo>>
*/
final class FileFilterIterator extends \FilterIterator
{
Expand All @@ -32,7 +34,7 @@ final class FileFilterIterator extends \FilterIterator
private CacheManagerInterface $cacheManager;

/**
* @var array<string,bool>
* @var array<string, bool>
*/
private array $visitedElements = [];

Expand Down
3 changes: 3 additions & 0 deletions src/Runner/FileLintingIterator.php
Expand Up @@ -33,6 +33,9 @@ final class FileLintingIterator extends \IteratorIterator

private LinterInterface $linter;

/**
* @param \Iterator<mixed, \SplFileInfo> $iterator
*/
public function __construct(\Iterator $iterator, LinterInterface $linter)
{
parent::__construct($iterator);
Expand Down
10 changes: 8 additions & 2 deletions src/Runner/Runner.php
Expand Up @@ -57,14 +57,15 @@ final class Runner
private $finder;

/**
* @var FixerInterface[]
* @var list<FixerInterface>
*/
private array $fixers;

private bool $stopOnViolation;

/**
* @param \Traversable<\SplFileInfo> $finder
* @param list<FixerInterface> $fixers
*/
public function __construct(
$finder,
Expand All @@ -90,6 +91,9 @@ public function __construct(
$this->stopOnViolation = $stopOnViolation;
}

/**
* @return array<string, array{appliedFixers: list<string>, diff: string}>
*/
public function fix(): array
{
$changed = [];
Expand All @@ -106,7 +110,6 @@ public function fix(): array
? new FileCachingLintingIterator($fileFilteredFileIterator, $this->linter)
: new FileLintingIterator($fileFilteredFileIterator, $this->linter);

/** @var \SplFileInfo $file */
foreach ($collection as $file) {
$fixInfo = $this->fixFile($file, $collection->currentLintingResult());

Expand All @@ -126,6 +129,9 @@ public function fix(): array
return $changed;
}

/**
* @return null|array{appliedFixers: list<string>, diff: string}
*/
private function fixFile(\SplFileInfo $file, LintingResultInterface $lintingResult): ?array
{
$name = $file->getPathname();
Expand Down

0 comments on commit b674087

Please sign in to comment.