Skip to content

Commit

Permalink
DX: automate checking 7.0 types on project itself
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Aug 4, 2021
1 parent 26e17c4 commit bd8ec6a
Show file tree
Hide file tree
Showing 40 changed files with 99 additions and 204 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ jobs:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: php vendor/bin/php-coveralls --verbose

- name: Run PHP CS Fixer for PHP 7.0 types
if: matrix.php-version == '7.1' # we run on lowest supported version, we running it on higher would falsy expect more changes, eg `mixed` type on PHP 8
run: php php-cs-fixer fix --diff --dry-run -v --config .php-cs-fixer.php70types.php

- name: Run PHP CS Fixer
env:
PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
'@PHPUnit75Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']],
'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']], // one should use PHPUnit built-in method instead
'header_comment' => ['header' => $header],
])
->setFinder($finder)
Expand Down
26 changes: 26 additions & 0 deletions .php-cs-fixer.php70types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

$config = require '.php-cs-fixer.dist.php';

$config->getFinder()->notPath([
// @TODO 4.0 change interface to be fully typehinted and remove the exceptions from this list
'src/DocBlock/Annotation.php',
'src/Tokenizer/Tokens.php',
]);

$config->setRules([
'phpdoc_to_param_type' => true, // EXPERIMENTAL rule, helping to ensure usage of 7.0+ typing
'phpdoc_to_return_type' => true, // EXPERIMENTAL rule, helping to ensure usage of 7.0+ typing
]);

return $config;
5 changes: 1 addition & 4 deletions src/Console/Command/ListSetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

/**
* @param string $format
*/
private function resolveReporterWithFactory($format, ReporterFactory $factory): ReporterInterface
private function resolveReporterWithFactory(string $format, ReporterFactory $factory): ReporterInterface
{
try {
$factory->registerBuiltInReporters();
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Report/ListSetsReport/JsonReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final class JsonReporter implements ReporterInterface
/**
* {@inheritdoc}
*/
public function getFormat()
public function getFormat(): string
{
return 'json';
}

/**
* {@inheritdoc}
*/
public function generate(ReportSummary $reportSummary)
public function generate(ReportSummary $reportSummary): string
{
$json = ['sets' => []];

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Report/ListSetsReport/ReportSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
/**
* @return RuleSetDescriptionInterface[]
*/
public function getSets()
public function getSets(): array
{
return $this->sets;
}
Expand Down
9 changes: 2 additions & 7 deletions src/Console/Report/ListSetsReport/ReporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,15 @@ public function registerReporter(ReporterInterface $reporter)
/**
* @return string[]
*/
public function getFormats()
public function getFormats(): array
{
$formats = array_keys($this->reporters);
sort($formats);

return $formats;
}

/**
* @param string $format
*
* @return ReporterInterface
*/
public function getReporter($format)
public function getReporter(string $format): ReporterInterface
{
if (!isset($this->reporters[$format])) {
throw new \UnexpectedValueException(sprintf('Reporter for format "%s" is not registered.', $format));
Expand Down
9 changes: 2 additions & 7 deletions src/Console/Report/ListSetsReport/ReporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@
*/
interface ReporterInterface
{
/**
* @return string
*/
public function getFormat();
public function getFormat(): string;

/**
* Process changed files array. Returns generated report.
*
* @return string
*/
public function generate(ReportSummary $reportSummary);
public function generate(ReportSummary $reportSummary): string;
}
4 changes: 2 additions & 2 deletions src/Console/Report/ListSetsReport/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final class TextReporter implements ReporterInterface
/**
* {@inheritdoc}
*/
public function getFormat()
public function getFormat(): string
{
return 'txt';
}

/**
* {@inheritdoc}
*/
public function generate(ReportSummary $reportSummary)
public function generate(ReportSummary $reportSummary): string
{
$output = '';
$i = 0;
Expand Down
23 changes: 5 additions & 18 deletions src/DocBlock/TypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ final class TypeExpression
private $namespaceUses;

/**
* @param string $value
* @param null|NamespaceAnalysis $namespace
* @param NamespaceUseAnalysis[] $namespaceUses
*/
public function __construct($value, $namespace, array $namespaceUses)
public function __construct(string $value, ?NamespaceAnalysis $namespace, array $namespaceUses)
{
while ('' !== $value && false !== $value) {
Preg::match(
Expand All @@ -144,15 +142,12 @@ public function __construct($value, $namespace, array $namespaceUses)
/**
* @return string[]
*/
public function getTypes()
public function getTypes(): array
{
return $this->types;
}

/**
* @return null|string
*/
public function getCommonType()
public function getCommonType(): ?string
{
$aliases = [
'true' => 'bool',
Expand Down Expand Up @@ -195,10 +190,7 @@ public function getCommonType()
return $mainType;
}

/**
* @return bool
*/
public function allowsNull()
public function allowsNull(): bool
{
foreach ($this->types as $type) {
if (\in_array($type, ['null', 'mixed'], true)) {
Expand Down Expand Up @@ -232,12 +224,7 @@ private function getParentType(string $type1, string $type2): ?string
return null;
}

/**
* @param string $type
*
* @return string
*/
private function normalize($type)
private function normalize(string $type): string
{
$aliases = [
'true' => 'bool',
Expand Down
5 changes: 1 addition & 4 deletions src/Fixer/ControlStructure/TrailingCommaInMultilineFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}
}

/**
* @param int $startIndex
*/
private function fixBlock(Tokens $tokens, $startIndex): void
private function fixBlock(Tokens $tokens, int $startIndex): void
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
if (!$tokensAnalyzer->isBlockMultiline($tokens, $startIndex)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private function getFunctionFilter(): callable
/**
* @return array<string, true> normalized function names of which the PHP compiler optimizes
*/
private function getAllCompilerOptimizedFunctionsNormalized()
private function getAllCompilerOptimizedFunctionsNormalized(): array
{
return $this->normalizeFunctionNames([
// @see https://github.com/php/php-src/blob/PHP-7.4/Zend/zend_compile.c "zend_try_compile_special_func"
Expand Down Expand Up @@ -396,7 +396,7 @@ private function getAllCompilerOptimizedFunctionsNormalized()
/**
* @return array<string, true> normalized function names of all internal defined functions
*/
private function getAllInternalFunctionsNormalized()
private function getAllInternalFunctionsNormalized(): array
{
return $this->normalizeFunctionNames(get_defined_functions()['internal']);
}
Expand Down
10 changes: 2 additions & 8 deletions src/Fixer/FunctionNotation/SingleLineThrowFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ private function trimNewLines(Tokens $tokens, int $startIndex, int $endIndex): v
}
}

/**
* @return bool
*/
private function isPreviousTokenToClear(Token $token)
private function isPreviousTokenToClear(Token $token): bool
{
static $tokens = null;

Expand All @@ -155,10 +152,7 @@ private function isPreviousTokenToClear(Token $token)
return $token->equalsAny($tokens) || $token->isObjectOperator();
}

/**
* @return bool
*/
private function isNextTokenToClear(Token $token)
private function isNextTokenToClear(Token $token): bool
{
static $tokens = null;

Expand Down
7 changes: 1 addition & 6 deletions src/Fixer/Import/GroupImportFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,8 @@ private function createNewGroup(Tokens $tokens, int $insertIndex, NamespaceUseAn

/**
* Check if namespace use analyses are different.
*
* @param null|NamespaceUseAnalysis $analysis1
* @param null|NamespaceUseAnalysis $analysis2
*
* @return bool
*/
private function areDeclarationsDifferent($analysis1, $analysis2)
private function areDeclarationsDifferent(?NamespaceUseAnalysis $analysis1, ?NamespaceUseAnalysis $analysis2): bool
{
if (null === $analysis1 || null === $analysis2) {
return true;
Expand Down
7 changes: 1 addition & 6 deletions src/Fixer/PhpUnit/PhpUnitExpectationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,7 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
}
}

/**
* @param int $startIndex
* @param int $endIndex
* @param int $objectOperator
*/
private function applyPhpUnitClassFixWithObjectOperator(Tokens $tokens, $startIndex, $endIndex, $objectOperator): void
private function applyPhpUnitClassFixWithObjectOperator(Tokens $tokens, int $startIndex, int $endIndex, int $objectOperator): void
{
$argumentsAnalyzer = new ArgumentsAnalyzer();

Expand Down
7 changes: 1 addition & 6 deletions src/Tokenizer/Analyzer/NamespacesAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ public function getDeclarations(Tokens $tokens): array
return $namespaces;
}

/**
* @param int $index
*
* @return NamespaceAnalysis
*/
public function getNamespaceAt(Tokens $tokens, $index)
public function getNamespaceAt(Tokens $tokens, int $index): NamespaceAnalysis
{
if (!$tokens->offsetExists($index)) {
throw new \InvalidArgumentException("Token index {$index} does not exist.");
Expand Down
7 changes: 1 addition & 6 deletions src/Tokenizer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ public function isArrayMultiLine(int $index): bool
return $this->isBlockMultiline($tokens, $index);
}

/**
* @param int $index
*
* @return bool
*/
public function isBlockMultiline(Tokens $tokens, $index)
public function isBlockMultiline(Tokens $tokens, int $index): bool
{
$blockType = Tokens::detectBlockType($tokens[$index]);

Expand Down
4 changes: 1 addition & 3 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,8 @@ public function testAllCodeContainSingleClassy(string $className): void

/**
* @dataProvider provideSrcClassCases
*
* @param string $className
*/
public function testThereIsNoTriggerErrorUsedDirectly($className): void
public function testThereIsNoTriggerErrorUsedDirectly(string $className): void
{
if (Utils::class === $className) {
$this->addToAssertionCount(1); // This is where "trigger_error" should be
Expand Down
5 changes: 1 addition & 4 deletions tests/Console/Command/ListSetsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function testListWithJsonFormat(): void
static::assertSame(0, $commandTester->getStatusCode());
}

/**
* @return CommandTester
*/
private function doTestExecute(array $arguments)
private function doTestExecute(array $arguments): CommandTester
{
$application = new Application();
$application->add(new ListSetsCommand());
Expand Down
30 changes: 6 additions & 24 deletions tests/Console/Report/ListSetsReport/AbstractReporterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,16 @@ final public function testGetFormat(): void
}

/**
* @param string $expectedReport
*
* @dataProvider provideGenerateCases
*/
final public function testGenerate($expectedReport, ReportSummary $reportSummary): void
final public function testGenerate(string $expectedReport, ReportSummary $reportSummary): void
{
$actualReport = $this->reporter->generate($reportSummary);

$this->assertFormat($expectedReport, $actualReport);
}

/**
* @return iterable
*/
final public function provideGenerateCases()
final public function provideGenerateCases(): iterable
{
yield 'example' => [
$this->createSimpleReport(),
Expand All @@ -80,24 +75,11 @@ final public function provideGenerateCases()
];
}

/**
* @return ReporterInterface
*/
abstract protected function createReporter();
abstract protected function createReporter(): ReporterInterface;

/**
* @return string
*/
abstract protected function getFormat();
abstract protected function getFormat(): string;

/**
* @param string $expected
* @param string $input
*/
abstract protected function assertFormat($expected, $input);
abstract protected function assertFormat(string $expected, string $input): void;

/**
* @return string
*/
abstract protected function createSimpleReport();
abstract protected function createSimpleReport(): string;
}

0 comments on commit bd8ec6a

Please sign in to comment.