Skip to content

Commit

Permalink
Add rector config (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Mar 6, 2023
1 parent 14827bd commit f65b1d0
Show file tree
Hide file tree
Showing 50 changed files with 255 additions and 286 deletions.
35 changes: 18 additions & 17 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text eol=lf

.github export-ignore
benchmarks export-ignore
tests export-ignore
examples export-ignore
phpstan export-ignore
.codecov.yml export-ignore
.gitattributes export-ignore
.gitignore export-ignore
CONTRIBUTING.md export-ignore
generate-class-reference.php export-ignore
mkdocs.yml export-ignore
phpbench.json export-ignore
.php-cs-fixer.php export-ignore
phpstan.neon.dist export-ignore
phpstan-baseline.neon export-ignore
phpunit.xml.dist export-ignore
renovate.json export-ignore
/.github export-ignore
/benchmarks export-ignore
/tests export-ignore
/examples export-ignore
/phpstan export-ignore
/.codecov.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/CONTRIBUTING.md export-ignore
/generate-class-reference.php export-ignore
/mkdocs.yml export-ignore
/phpbench.json export-ignore
/.php-cs-fixer.php export-ignore
/phpstan.neon.dist export-ignore
/phpstan-baseline.neon export-ignore
/phpunit.xml.dist export-ignore
/rector.php export-ignore
/renovate.json export-ignore
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ help: ## Displays this list of targets with descriptions
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: fix
fix: vendor
composer fix
fix: rector php-cs-fixer ## Automatic code fixes

.PHONY: rector
rector: vendor ## Automatic code fixes with Rector
composer rector

.PHONY: php-cs-fixer
php-cs-fixer: vendor ## Fix code style
composer php-cs-fixer

.PHONY: stan
stan: ## Runs static analysis with phpstan
Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"psr/http-message": "^1",
"react/http": "^1.6",
"react/promise": "^2.9",
"rector/rector": "^0.15.18",
"symfony/polyfill-php81": "^1.23",
"symfony/var-exporter": "^5 || ^6",
"thecodingmachine/safe": "^1.3 || ^2"
Expand Down Expand Up @@ -59,9 +60,18 @@
},
"scripts": {
"bench": "phpbench run",
"check": "composer fix && composer stan && composer test",
"check": [
"@fix",
"@stan",
"@test"
],
"docs": "php generate-class-reference.php",
"fix": "php-cs-fixer fix",
"fix": [
"@rector",
"@php-cs-fixer"
],
"php-cs-fixer": "php-cs-fixer fix",
"rector": "rector process",
"stan": "phpstan",
"test": "phpunit"
}
Expand Down
8 changes: 3 additions & 5 deletions examples/01-blog/Blog/Type/Field/HtmlField.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public static function build(array $config): array
$html = $resolver($rootValue, $args);
$text = \strip_tags($html);

if (isset($args['maxLength'])) {
$safeText = \mb_substr($text, 0, $args['maxLength']);
} else {
$safeText = $text;
}
$safeText = isset($args['maxLength'])
? \mb_substr($text, 0, $args['maxLength'])
: $text;

switch ($args['format']) {
case ContentFormatType::FORMAT_HTML:
Expand Down
6 changes: 3 additions & 3 deletions generate-class-reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function renderClass(ReflectionClass $class, array $options): string
$constants[] = "const {$name} = " . VarExporter::export($value) . ';';
}

if (count($constants) > 0) {
if ($constants !== []) {
$constants = "```php\n" . implode("\n", $constants) . "\n```";
$content .= "### {$className} Constants\n\n{$constants}\n\n";
}
Expand All @@ -92,7 +92,7 @@ function renderClass(ReflectionClass $class, array $options): string
}
}

if (count($props) > 0) {
if ($props !== []) {
$props = "```php\n" . implode("\n\n", $props) . "\n```";
$content .= "### {$className} Props\n\n{$props}\n\n";
}
Expand All @@ -106,7 +106,7 @@ function renderClass(ReflectionClass $class, array $options): string
}
}

if (count($methods) > 0) {
if ($methods !== []) {
$methods = implode("\n\n", $methods);
$content .= "### {$className} Methods\n\n{$methods}\n\n";
}
Expand Down
30 changes: 30 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

use Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector;
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
SetList::CODE_QUALITY,
]);
$rectorConfig->skip([
CallableThisArrayToAnonymousFunctionRector::class, // Callable in array form is shorter and more efficient
IssetOnPropertyObjectToPropertyExistsRector::class, // isset() is nice when moving towards typed properties
FlipTypeControlToUseExclusiveTypeRector::class, // Unnecessarily complex with PHPStan
UnusedForeachValueToArrayKeysRector::class, // Less efficient
]);
$rectorConfig->paths([
__DIR__ . '/examples',
__DIR__ . '/phpstan',
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/.php-cs-fixer.php',
__DIR__ . '/generate-class-reference.php',
__DIR__ . '/rector.php',
]);
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
};
10 changes: 5 additions & 5 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct(
$this->positions = $positions;
$this->path = $path;

if (\is_array($extensions) && \count($extensions) > 0) {
if (\is_array($extensions) && $extensions !== []) {
$this->extensions = $extensions;
} elseif ($previous instanceof ProvidesExtensions) {
$this->extensions = $previous->getExtensions();
Expand Down Expand Up @@ -167,9 +167,9 @@ protected function isLocated(): bool
$nodes = $this->getNodes();

return $path !== null
&& \count($path) > 0
&& $path !== []
&& $nodes !== null
&& \count($nodes) > 0;
&& $nodes !== [];
}

public function isClientSafe(): bool
Expand Down Expand Up @@ -227,11 +227,11 @@ public function getLocations(): array
$nodes = $this->getNodes();

$this->locations = [];
if ($source !== null && \count($positions) !== 0) {
if ($source !== null && $positions !== []) {
foreach ($positions as $position) {
$this->locations[] = $source->getLocation($position);
}
} elseif ($nodes !== null && \count($nodes) !== 0) {
} elseif ($nodes !== null && $nodes !== []) {
foreach ($nodes as $node) {
if (isset($node->loc->source)) {
$this->locations[] = $node->loc->source->getLocation($node->loc->start);
Expand Down
10 changes: 5 additions & 5 deletions src/Error/FormattedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function printError(Error $error): string
$printedLocations = [];

$nodes = $error->nodes;
if (isset($nodes) && \count($nodes) > 0) {
if (isset($nodes) && $nodes !== []) {
foreach ($nodes as $node) {
$location = $node->loc;
if (isset($location)) {
Expand All @@ -55,14 +55,14 @@ public static function printError(Error $error): string
}
}
}
} elseif ($error->getSource() !== null && \count($error->getLocations()) !== 0) {
} elseif ($error->getSource() !== null && $error->getLocations() !== []) {
$source = $error->getSource();
foreach ($error->getLocations() as $location) {
$printedLocations[] = self::highlightSourceAtLocation($source, $location);
}
}

return \count($printedLocations) === 0
return $printedLocations === []
? $error->getMessage()
: \implode("\n\n", \array_merge([$error->getMessage()], $printedLocations)) . "\n";
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public static function createFromException(\Throwable $exception, int $debugFlag
static fn (SourceLocation $loc): array => $loc->toSerializableArray(),
$exception->getLocations()
);
if (\count($locations) > 0) {
if ($locations !== []) {
$formattedError['locations'] = $locations;
}

Expand All @@ -152,7 +152,7 @@ public static function createFromException(\Throwable $exception, int $debugFlag

if ($exception instanceof ProvidesExtensions) {
$extensions = $exception->getExtensions();
if (\is_array($extensions) && \count($extensions) > 0) {
if (\is_array($extensions) && $extensions !== []) {
$formattedError['extensions'] = $extensions;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Executor/ExecutionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function toArray(int $debug = DebugFlag::NONE): array
{
$result = [];

if (\count($this->errors) > 0) {
if ($this->errors !== []) {
$errorsHandler = $this->errorsHandler
?? static fn (array $errors, callable $formatter): array => \array_map($formatter, $errors);

Expand All @@ -176,7 +176,7 @@ public function toArray(int $debug = DebugFlag::NONE): array
$result['data'] = $this->data;
}

if ($this->extensions !== null && \count($this->extensions) > 0) {
if ($this->extensions !== null && $this->extensions !== []) {
$result['extensions'] = $this->extensions;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected static function buildExecutionContext(
}
}

if (\count($errors) > 0) {
if ($errors !== []) {
return $errors;
}

Expand Down Expand Up @@ -1095,7 +1095,7 @@ protected function defaultTypeResolver($value, $contextValue, ResolveInfo $info,
}
}

if (\count($promisedIsTypeOfResults) > 0) {
if ($promisedIsTypeOfResults !== []) {
return $this->exeContext->promiseAdapter
->all($promisedIsTypeOfResults)
->then(static function ($isTypeOfResults) use ($possibleTypes): ?ObjectType {
Expand Down
10 changes: 4 additions & 6 deletions src/Executor/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,9 @@ public static function getVariableValues(Schema $schema, NodeList $varDefNodes,
}
}

if (\count($errors) > 0) {
return [$errors, null];
}

return [null, $coercedValues];
return $errors === []
? [null, $coercedValues]
: [$errors, null];
}

/**
Expand Down Expand Up @@ -176,7 +174,7 @@ public static function getDirectiveValues(Directive $directiveDef, Node $node, ?
*/
public static function getArgumentValues($def, Node $node, ?array $variableValues = null): array
{
if (\count($def->args) === 0) {
if ($def->args === []) {
return [];
}

Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function promiseToExecute(
? $source
: Parser::parse(new Source($source, 'GraphQL'));

if ($validationRules === null || \count($validationRules) === 0) {
if ($validationRules === null || $validationRules === []) {
$queryComplexity = DocumentValidator::getRule(QueryComplexity::class);
assert($queryComplexity instanceof QueryComplexity, 'should not register a different rule for QueryComplexity');

Expand All @@ -142,7 +142,7 @@ public static function promiseToExecute(

$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules);

if (\count($validationErrors) > 0) {
if ($validationErrors !== []) {
return $promiseAdapter->createFulfilled(
new ExecutionResult(null, $validationErrors)
);
Expand Down
4 changes: 0 additions & 4 deletions src/Language/AST/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ abstract class Node implements \JsonSerializable
*/
public function __construct(array $vars)
{
if (\count($vars) === 0) {
return;
}

Utils::assign($this, $vars);
}

Expand Down
13 changes: 5 additions & 8 deletions src/Language/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ class Lexer
/**
* The (1-indexed) line containing the current token.
*/
public int $line;
public int $line = 1;

/**
* The character offset at which the current line begins.
*/
public int $lineStart;
public int $lineStart = 0;

/**
* Current cursor position for UTF8 encoding of the source.
*/
private int $position;
private int $position = 0;

/**
* Current cursor position for ASCII representation of the source.
*/
private int $byteStreamPosition;
private int $byteStreamPosition = 0;

/**
* @phpstan-param ParserOptions $options
* @phpstan-param ParserOptions $options
*/
public function __construct(Source $source, array $options = [])
{
Expand All @@ -80,9 +80,6 @@ public function __construct(Source $source, array $options = [])
$this->options = $options;
$this->lastToken = $startOfFileToken;
$this->token = $startOfFileToken;
$this->line = 1;
$this->lineStart = 0;
$this->position = $this->byteStreamPosition = 0;
}

public function advance(): Token
Expand Down
Loading

0 comments on commit f65b1d0

Please sign in to comment.