Skip to content

Commit

Permalink
Add rector, upgrade codebase to using PHP 7.4 features
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Oct 2, 2024
1 parent 2b56112 commit 63a2d64
Show file tree
Hide file tree
Showing 32 changed files with 173 additions and 257 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"friendsofphp/php-cs-fixer": "^3.62.0",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^4.23"
"rector/rector": "^1.0",
"vimeo/psalm": "^4.30"
},
"suggest": {
"doctrine/annotations": "^2.0"
Expand All @@ -79,6 +80,7 @@
},
"scripts-descriptions": {
"cs": "Fix all codestyle issues",
"rector": "Automatic refactoring",
"lint": "Test codestyle",
"test": "Run all non-legacy and codestyle tests",
"testlegacy": "Run tests using the legacy TokenAnalyser",
Expand All @@ -93,7 +95,11 @@
},
"scripts": {
"cs": "export XDEBUG_MODE=off && php-cs-fixer fix --allow-risky=yes",
"lint": "@cs --dry-run",
"rector": "rector process src",
"lint": [
"@cs --dry-run",
"@rector --dry-run"
],
"test": [
"export XDEBUG_MODE=off && phpunit",
"@lint"
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ parameters:
count: 1
path: src/Annotations/Schema.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: src/Generator.php

-
message: "#^Result of && is always false\\.$#"
count: 3
Expand Down
34 changes: 34 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector;
use Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withRules([
TypedPropertyFromStrictConstructorRector::class
])
->withSkip([
CombineIfRector::class,
ExplicitBoolCompareRector::class,
ExplicitReturnNullRector::class => [
__DIR__ . '/src/Analysers/TokenAnalyser.php',
],
ForRepeatedCountToOwnVariableRector::class,
RemoveAlwaysTrueIfConditionRector::class => [
__DIR__ . '/src/Processors/ExpandEnums.php',
] ,
RemoveDeadInstanceOfRector::class => [
__DIR__ . '/src/Processors/ExpandEnums.php',
],
ShortenElseIfRector::class,
])
->withPreparedSets(true, true)
->withPhpVersion(PhpVersion::PHP_74);
14 changes: 3 additions & 11 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@

class AttributeAnnotationFactory implements AnnotationFactoryInterface
{
/** @var Generator|null */
protected $generator;
use GeneratorAwareTrait;

public function isSupported(): bool
{
return \PHP_VERSION_ID >= 80100;
}

public function setGenerator(Generator $generator): void
{
$this->generator = $generator;
}

public function build(\Reflector $reflector, Context $context): array
{
if (!$this->isSupported() || !method_exists($reflector, 'getAttributes')) {
Expand Down Expand Up @@ -116,9 +110,7 @@ public function build(\Reflector $reflector, Context $context): array
Generator::$context = null;
}

$annotations = array_values(array_filter($annotations, function ($a) {
return $a instanceof OA\AbstractAnnotation;
}));
$annotations = array_values(array_filter($annotations, fn ($a) => $a instanceof OA\AbstractAnnotation));

// merge backwards into parents...
$isParent = function (OA\AbstractAnnotation $annotation, OA\AbstractAnnotation $possibleParent): bool {
Expand All @@ -140,7 +132,7 @@ public function build(\Reflector $reflector, Context $context): array
}

// Property can be nested...
return $annotation->getRoot() != $possibleParent->getRoot()
return $annotation->getRoot() !== $possibleParent->getRoot()
&& ($explicitParent || ($isAttachable && $isParentAllowed));
};

Expand Down
6 changes: 2 additions & 4 deletions src/Analysers/DocBlockAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

class DocBlockAnnotationFactory implements AnnotationFactoryInterface
{
/** @var DocBlockParser|null */
protected $docBlockParser = null;
use GeneratorAwareTrait;

/** @var Generator|null */
protected $generator = null;
protected ?DocBlockParser $docBlockParser = null;

public function __construct(?DocBlockParser $docBlockParser = null)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Analysers/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
class DocBlockParser
{
/**
* @var DocParser
*/
protected $docParser;
protected DocParser $docParser;

/**
* @param array<string, class-string> $aliases
Expand Down
19 changes: 19 additions & 0 deletions src/Analysers/GeneratorAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Analysers;

use OpenApi\Generator;

trait GeneratorAwareTrait
{
protected ?Generator $generator = null;

public function setGenerator(Generator $generator): void
{
$this->generator = $generator;
}
}
14 changes: 5 additions & 9 deletions src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@
*/
class ReflectionAnalyser implements AnalyserInterface
{
/** @var AnnotationFactoryInterface[] */
protected $annotationFactories;
use GeneratorAwareTrait;

/** @var Generator|null */
protected $generator;
/** @var AnnotationFactoryInterface[] */
protected array $annotationFactories = [];

/**
* @param array<AnnotationFactoryInterface> $annotationFactories
*/
public function __construct(array $annotationFactories = [])
{
$this->annotationFactories = [];
foreach ($annotationFactories as $annotationFactory) {
if ($annotationFactory->isSupported()) {
$this->annotationFactories[] = $annotationFactory;
Expand Down Expand Up @@ -113,13 +111,11 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
'methods' => [],
'context' => $context,
];
$normaliseClass = function (string $name): string {
return '\\' . ltrim($name, '\\');
};
$normaliseClass = fn (string $name): string => '\\' . ltrim($name, '\\');
if ($parentClass = $rc->getParentClass()) {
$definition['extends'] = $normaliseClass($parentClass->getName());
}
$definition[$contextType == 'class' ? 'implements' : 'extends'] = array_map($normaliseClass, $details['interfaces']);
$definition[$contextType === 'class' ? 'implements' : 'extends'] = array_map($normaliseClass, $details['interfaces']);
$definition['traits'] = array_map($normaliseClass, $details['traits']);

foreach ($this->annotationFactories as $annotationFactory) {
Expand Down
16 changes: 7 additions & 9 deletions src/Analysers/TokenAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
*/
class TokenAnalyser implements AnalyserInterface
{
/** @var Generator|null */
protected $generator;

public function setGenerator(Generator $generator): void
{
$this->generator = $generator;
}
use GeneratorAwareTrait;

/**
* Extract and process all doc-comments from a file.
Expand Down Expand Up @@ -166,7 +160,9 @@ protected function fromTokens(array $tokens, Context $parseContext): Analysis

if ($token[0] === T_IMPLEMENTS) {
$schemaContext->implements = $this->parseNamespaceList($tokens, $token, $parseContext);
$classDefinition['implements'] = array_map([$schemaContext, 'fullyQualifiedName'], $schemaContext->implements);
$classDefinition['implements'] = array_map(function (?string $source) use ($schemaContext): string {
return $schemaContext->fullyQualifiedName($source);
}, $schemaContext->implements);
}

if ($comment) {
Expand Down Expand Up @@ -207,7 +203,9 @@ protected function fromTokens(array $tokens, Context $parseContext): Analysis

if ($token[0] === T_EXTENDS) {
$schemaContext->extends = $this->parseNamespaceList($tokens, $token, $parseContext);
$interfaceDefinition['extends'] = array_map([$schemaContext, 'fullyQualifiedName'], $schemaContext->extends);
$interfaceDefinition['extends'] = array_map(function (?string $source) use ($schemaContext): string {
return $schemaContext->fullyQualifiedName($source);
}, $schemaContext->extends);
}

if ($comment) {
Expand Down
10 changes: 5 additions & 5 deletions src/Analysers/TokenScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function scanTokens(array $tokens): array
break;
case '}':
array_pop($stack);
if (count($stack) == $unitLevel) {
if (count($stack) === $unitLevel) {
$currentName = null;
}
break;
Expand All @@ -76,7 +76,7 @@ protected function scanTokens(array $tokens): array

switch ($token[0]) {
case T_ABSTRACT:
if (count($stack)) {
if ($stack !== []) {
$isAbstractFunction = true;
}
break;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function scanTokens(array $tokens): array
// unless ...
if (is_string($token) && ($token === '(' || $token === '{')) {
// new class[()] { ... }
if ('{' == $token) {
if ('{' === $token) {
prev($tokens);
}
break;
Expand Down Expand Up @@ -212,7 +212,7 @@ protected function scanTokens(array $tokens): array
/**
* Get the next token that is not whitespace or comment.
*
* @return string|array|false
* @return string|array|false Next token
*/
protected function nextToken(array &$tokens)
{
Expand Down Expand Up @@ -254,7 +254,7 @@ protected function resolveFQN(array $names, string $namespace, array $uses): arr
protected function skipTo(array &$tokens, string $char, bool $prev = false): void
{
while (false !== ($token = next($tokens))) {
if (is_string($token) && $token == $char) {
if (is_string($token) && $token === $char) {
if ($prev) {
prev($tokens);
}
Expand Down
38 changes: 11 additions & 27 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,34 @@
*/
class Analysis
{
/**
* @var \SplObjectStorage
*/
public $annotations;
public \SplObjectStorage $annotations;

/**
* Class definitions.
*
* @var array
*/
public $classes = [];
public array $classes = [];

/**
* Interface definitions.
*
* @var array
*/
public $interfaces = [];
public array $interfaces = [];

/**
* Trait definitions.
*
* @var array
*/
public $traits = [];
public array $traits = [];

/**
* Enum definitions.
*
* @var array
*/
public $enums = [];
public array $enums = [];

/**
* The target OpenApi annotation.
*
* @var OA\OpenApi|null
*/
public $openapi = null;
public ?OA\OpenApi $openapi = null;

/**
* @var Context|null
*/
public $context = null;
public ?Context $context = null;

public function __construct(array $annotations = [], ?Context $context = null)
{
Expand Down Expand Up @@ -150,7 +134,7 @@ public function addAnalysis(Analysis $analysis): void
$this->interfaces = array_merge($this->interfaces, $analysis->interfaces);
$this->traits = array_merge($this->traits, $analysis->traits);
$this->enums = array_merge($this->enums, $analysis->enums);
if ($this->openapi === null && $analysis->openapi !== null) {
if (!$this->openapi instanceof OA\OpenApi && $analysis->openapi instanceof OA\OpenApi) {
$this->openapi = $analysis->openapi;
}
}
Expand Down Expand Up @@ -347,7 +331,7 @@ public function getAnnotationForSource(string $fqdn, string $class): ?OA\Abstrac
if (is_iterable($definition['context']->annotations)) {
/** @var OA\AbstractAnnotation $annotation */
foreach (array_reverse($definition['context']->annotations) as $annotation) {
if (is_a($annotation, $class) && $annotation->isRoot($class) && !$annotation->_context->is('generated')) {
if ($annotation instanceof $class && $annotation->isRoot($class) && !$annotation->_context->is('generated')) {
return $annotation;
}
}
Expand Down Expand Up @@ -379,7 +363,7 @@ public function getContext(object $annotation): ?Context
*/
public function merged(): Analysis
{
if ($this->openapi === null) {
if (!$this->openapi instanceof OA\OpenApi) {
throw new OpenApiException('No openapi target set. Run the MergeIntoOpenApi processor');
}
$unmerged = $this->openapi->_unmerged;
Expand Down Expand Up @@ -436,7 +420,7 @@ public function process($processors = null): void

public function validate(): bool
{
if ($this->openapi !== null) {
if ($this->openapi instanceof OA\OpenApi) {
return $this->openapi->validate();
}

Expand Down
Loading

0 comments on commit 63a2d64

Please sign in to comment.