Skip to content

Commit

Permalink
update to new phpstan rules, add static fixes (#18)
Browse files Browse the repository at this point in the history
* update to new phpstan rules, add static fixes

* [ci-review] Rector Rectify

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
TomasVotruba and kaizen-ci committed May 11, 2021
1 parent 70ef6eb commit f8b0ac3
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 70 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"phpstan/phpstan-nette": "^0.12.16",
"phpunit/phpunit": "^9.5",
"rector/rector-generator": "^0.1.7",
"rector/rector-phpstan-rules": "^0.1",
"rector/rector-phpstan-rules": "^0.2.3",
"symplify/coding-standard": "^9.3.5",
"symplify/easy-ci": "^9.3.5",
"symplify/easy-coding-standard": "^9.3.5",
Expand Down
4 changes: 0 additions & 4 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\PhpParser\Parser\NikicPhpParserFactory;
use Rector\Core\PhpParser\Parser\PhpParserLexerFactory;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\EditorConfig\EditorConfigIdiosyncraticParser;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
Expand Down Expand Up @@ -155,6 +153,4 @@
$services->set(Formatter::class);

$services->set(EditorConfig::class);

$services->alias(EditorConfigParserInterface::class, EditorConfigIdiosyncraticParser::class);
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<?php

namespace Rector\Tests\FileFormatter\EditorConfig\EditorConfigIdiosyncraticParser;
declare(strict_types=1);

namespace Rector\Tests\FileFormatter\EditorConfig\EditorConfigParser;

use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\EditorConfig\EditorConfigParser;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class EditorConfigIdiosyncraticParserTest extends AbstractTestCase
final class EditorConfigParserTest extends AbstractTestCase
{
/**
* @var EditorConfigParserInterface
* @var EditorConfigParser
*/
private $editorConfigParser;

protected function setUp(): void
{
$this->boot();
$this->editorConfigParser = $this->getService(EditorConfigParserInterface::class);
$this->editorConfigParser = $this->getService(EditorConfigParser::class);
}

public function testComposerJsonFile(): void
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use Idiosyncratic\EditorConfig\EditorConfig;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
use Rector\FileFormatter\ValueObject\EditorConfigOption;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;

/**
* @see \Rector\Tests\FileFormatter\EditorConfig\EditorConfigIdiosyncraticParser\EditorConfigIdiosyncraticParserTest
* @see \Rector\Tests\FileFormatter\EditorConfig\EditorConfigParser\EditorConfigParserTest
*/
final class EditorConfigIdiosyncraticParser implements EditorConfigParserInterface
final class EditorConfigParser
{
public function __construct(
private EditorConfig $editorConfig
Expand All @@ -27,32 +27,34 @@ public function extractConfigurationForFile(
$smartFileInfo = $file->getSmartFileInfo();
$configuration = $this->editorConfig->getConfigForPath($smartFileInfo->getRealPath());

if (array_key_exists(self::INDENT_STYLE, $configuration)) {
$indentStyle = (string) $configuration[self::INDENT_STYLE]->getValue();
if (array_key_exists(EditorConfigOption::INDENT_STYLE, $configuration)) {
$indentStyle = (string) $configuration[EditorConfigOption::INDENT_STYLE]->getValue();

$editorConfigConfigurationBuilder->withIndentStyle($indentStyle);
}

if (array_key_exists(self::INDENT_SIZE, $configuration)) {
$indentSize = (int) $configuration[self::INDENT_SIZE]->getValue();
if (array_key_exists(EditorConfigOption::INDENT_SIZE, $configuration)) {
$indentSize = (int) $configuration[EditorConfigOption::INDENT_SIZE]->getValue();

$editorConfigConfigurationBuilder->withIndentSize($indentSize);
}

if (array_key_exists(self::END_OF_LINE, $configuration)) {
$endOfLine = (string) $configuration[self::END_OF_LINE]->getValue();
if (array_key_exists(EditorConfigOption::END_OF_LINE, $configuration)) {
$endOfLine = (string) $configuration[EditorConfigOption::END_OF_LINE]->getValue();

$editorConfigConfigurationBuilder->withEndOfLineFromEditorConfig($endOfLine);
}

if (array_key_exists(self::INSERT_FINAL_NEWLINE, $configuration)) {
$insertFinalNewline = (bool) $configuration[self::INSERT_FINAL_NEWLINE]->getValue();
if (array_key_exists(EditorConfigOption::INSERT_FINAL_NEWLINE, $configuration)) {
$insertFinalNewline = (bool) $configuration[EditorConfigOption::INSERT_FINAL_NEWLINE]->getValue();

$editorConfigConfigurationBuilder->withInsertFinalNewline($insertFinalNewline);
}

if (array_key_exists(self::TAB_WIDTH, $configuration)) {
$editorConfigConfigurationBuilder->withIndentSize($configuration[self::TAB_WIDTH]->getValue());
if (array_key_exists(EditorConfigOption::TAB_WIDTH, $configuration)) {
$editorConfigConfigurationBuilder->withIndentSize(
$configuration[EditorConfigOption::TAB_WIDTH]->getValue()
);
}

return $editorConfigConfigurationBuilder->build();
Expand Down
8 changes: 4 additions & 4 deletions packages/FileFormatter/FileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface;
use Rector\FileFormatter\EditorConfig\EditorConfigParser;
use Rector\FileFormatter\Exception\InvalidNewLineStringException;
use Rector\FileFormatter\Exception\ParseIndentException;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
Expand All @@ -22,7 +22,7 @@ final class FileFormatter
* @param FileFormatterInterface[] $fileFormatters
*/
public function __construct(
private EditorConfigParserInterface $editorConfigParser,
private EditorConfigParser $editorConfigParser,
private ParameterProvider $parameterProvider,
private array $fileFormatters = []
) {
Expand Down Expand Up @@ -62,13 +62,13 @@ private function sniffOriginalFileContent(
try {
$indent = Indent::fromContent($file->getOriginalFileContent());
$editorConfigConfigurationBuilder->withIndent($indent);
} catch (ParseIndentException $parseIndentException) {
} catch (ParseIndentException) {
}

try {
$newLine = NewLine::fromContent($file->getOriginalFileContent());
$editorConfigConfigurationBuilder->withNewLine($newLine);
} catch (InvalidNewLineStringException $invalidNewLineStringException) {
} catch (InvalidNewLineStringException) {
}
}

Expand Down
33 changes: 33 additions & 0 deletions packages/FileFormatter/ValueObject/EditorConfigOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\FileFormatter\ValueObject;

final class EditorConfigOption
{
/**
* @var string
*/
public const INDENT_STYLE = 'indent_style';

/**
* @var string
*/
public const INDENT_SIZE = 'indent_size';

/**
* @var string
*/
public const END_OF_LINE = 'end_of_line';

/**
* @var string
*/
public const INSERT_FINAL_NEWLINE = 'insert_final_newline';

/**
* @var string
*/
public const TAB_WIDTH = 'tab_width';
}
10 changes: 8 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ includes:
- vendor/symplify/phpstan-rules/config/code-complexity-rules.neon
- vendor/symplify/phpstan-rules/config/doctrine-rules.neon
- vendor/symplify/phpstan-rules/config/forbidden-static-rules.neon
- vendor/symplify/phpstan-rules/config/generic-rules.neon
- vendor/symplify/phpstan-rules/config/naming-rules.neon
- vendor/symplify/phpstan-rules/config/regex-rules.neon
- vendor/symplify/phpstan-rules/config/services-rules.neon
Expand All @@ -17,7 +16,6 @@ parameters:
level: max

paths:
- ecs.php
- rector.php
- bin
- src
Expand Down Expand Up @@ -505,3 +503,11 @@ parameters:
-
message: '#Do not call parent method if parent method is empty#'
path: packages/BetterPhpDocParser/PhpDoc/DoctrineAnnotationTagValueNode.php

# upgrade to PHP 7.4 wip
- '#This property type might be inlined to PHP\. Do you have confidence it is correct\? Put it here#'

# special case - cleanup in the future
-
message: '#File processor must require Rector rules in constructor via TypeRectorInterface\[\] \$typeRectors array autowire#'
path: src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SplArrayFixedTypeNarrower
{
public function narrow(Type $paramType): Type
{
if ($paramType->isSuperTypeOf(new ObjectType('SplArrayFixed'))->no()) {
if ($paramType->isSuperTypeOf(new ObjectType('SplFixedArray'))->no()) {
return $paramType;
}

Expand Down
14 changes: 14 additions & 0 deletions stubs/PhpCsFixer/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpCsFixer\Tokenizer;

if (class_exists('PhpCsFixer\Tokenizer\Tokens')) {
return;
}

final class Tokens extends \SplFixedArray
{

}

0 comments on commit f8b0ac3

Please sign in to comment.