Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
<?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\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withRules([
TypedPropertyFromStrictConstructorRector::class
])
->withSkip([
CombineIfRector::class,
ExplicitBoolCompareRector::class,
ForRepeatedCountToOwnVariableRector::class,
RemoveAlwaysTrueIfConditionRector::class => [
__DIR__ . '/src/Processors/ExpandEnums.php',
] ,
],
RemoveDeadInstanceOfRector::class => [
__DIR__ . '/src/Processors/ExpandEnums.php',
],
ShortenElseIfRector::class,
NewlineAfterStatementRector::class,
NullableCompareToNullRector::class,
StringClassNameToClassConstantRector::class => [
__DIR__ . '/src/Analysers/DocBlockParser.php',
],
EncapsedStringsToSprintfRector::class,
ParamTypeByMethodCallTypeRector::class => [
__DIR__ . '/src/Serializer.php',
],
])
->withPreparedSets(true, true)
->withPhpVersion(PhpVersion::PHP_74);
->withPreparedSets(true, true, true, true)
->withPhpVersion(PhpVersion::PHP_74)
->withPhpSets();
2 changes: 1 addition & 1 deletion src/Analysers/ComposerAutoloaderScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ComposerAutoloaderScanner
public function scan(array $namespaces): array
{
$units = [];
if ($autoloader = $this->getComposerAutoloader()) {
if ($autoloader = static::getComposerAutoloader()) {
foreach (array_keys($autoloader->getClassMap()) as $unit) {
foreach ($namespaces as $namespace) {
if (0 === strpos($unit, $namespace)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Analysers/DocBlockAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function isSupported(): bool
return DocBlockParser::isEnabled();
}

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

Expand Down
10 changes: 5 additions & 5 deletions src/Analysers/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ public function fromComment(string $comment, Context $context): array
}

return $this->docParser->parse($comment, $context->getDebugLocation());
} catch (\Exception $e) {
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote((string) $context, '/') . '\.$/', $e->getMessage(), $matches)) {
} catch (\Exception $exception) {
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote((string) $context, '/') . '\.$/', $exception->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = (int) $matches[2];
$atPos = strpos($comment, '@');
$context->line -= substr_count($comment, "\n", $atPos + $errorPos) + 1;
$lines = explode("\n", substr($comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1; // position starts at 0 character starts at 1
$context->logger->error($errorMessage . ' in ' . $context, ['exception' => $e]);
$context->logger->error($errorMessage . ' in ' . $context, ['exception' => $exception]);
} else {
$context->logger->error(
$e->getMessage() . ($context->filename ? ('; file=' . $context->filename) : ''),
['exception' => $e]
$exception->getMessage() . ($context->filename ? ('; file=' . $context->filename) : ''),
['exception' => $exception]
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Analysers/TokenScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function scanFile(string $filename): array
$parser = (new ParserFactory())->createForNewestSupportedVersion();
try {
$stmts = $parser->parse(file_get_contents($filename));
} catch (Error $e) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
} catch (Error $error) {
throw new \RuntimeException($error->getMessage(), $error->getCode(), $error);
}

$result = [];
Expand All @@ -59,7 +59,7 @@ protected function collect_stmts(array $stmts, string $namespace): array

return $namespace . '\\' . $name;
};
$details = function () use (&$uses) {
$details = function () use (&$uses): array {
return [
'uses' => $uses,
'interfaces' => [],
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function unmerged(): Analysis
*
* @return \stdClass {merged: Analysis, unmerged: Analysis}
*/
public function split()
public function split(): \stdClass
{
$result = new \stdClass();
$result->merged = $this->merged();
Expand Down
4 changes: 2 additions & 2 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function jsonSerialize()
* @param string $ref Current ref path?
* @param object $context a free-form context contains
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
if (in_array($this, $skip, true)) {
return true;
Expand Down Expand Up @@ -792,6 +792,6 @@ protected function combine(...$args): array
}
}

return array_filter($combined, fn ($value) => !Generator::isDefault($value) && $value !== null);
return array_filter($combined, fn ($value): bool => !Generator::isDefault($value) && $value !== null);
}
}
2 changes: 1 addition & 1 deletion src/Annotations/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Components extends AbstractAnnotation
*/
public static function componentTypes(): array
{
return array_filter(array_keys(self::$_nested), fn ($value) => $value !== Attachable::class);
return array_filter(array_keys(self::$_nested), fn ($value): bool => $value !== Attachable::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Items extends Schema
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
if (in_array($this, $skip, true)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function jsonSerialize()
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
$valid = parent::validate($stack, $skip, $ref, $context);

Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function ref(string $ref)
throw new OpenApiException('Unsupported $ref "' . $ref . '", it should start with "#/"');
}

return $this->resolveRef($ref, '#/', $this, []);
return self::resolveRef($ref, '#/', $this, []);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function jsonSerialize()
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
if (in_array($this, $skip, true)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class Parameter extends AbstractAnnotation
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
if (in_array($this, $skip, true)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Response extends AbstractAnnotation
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
$valid = parent::validate($stack, $skip, $ref, $context);

Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function jsonSerialize()
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
{
if ($this->type === 'array' && Generator::isDefault($this->items)) {
$this->_context->logger->warning('@OA\\Items() is required when ' . $this->identity() . ' has type "array" in ' . $this->_context);
Expand Down
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function __get(string $property)
return null;
}

public function __toString()
public function __toString(): string
{
return $this->getDebugLocation();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function getProcessorPipeline(): Pipeline
}

$config = $this->getConfig();
$walker = function (callable $pipe) use ($config) {
$walker = function (callable $pipe) use ($config): void {
$rc = new \ReflectionClass($pipe);

// apply config
Expand Down
4 changes: 1 addition & 3 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public function remove($pipe = null, ?callable $matcher = null): Pipeline
// allow matching on class name in $pipe in a string
if (is_string($pipe) && !$matcher) {
$pipeClass = $pipe;
$matcher = function ($pipe) use ($pipeClass) {
return !$pipe instanceof $pipeClass;
};
$matcher = (fn ($pipe): bool => !$pipe instanceof $pipeClass);
}

if ($matcher) {
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AugmentDiscriminators.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class AugmentDiscriminators
{
public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
/** @var OA\Discriminator[] $discriminators */
$discriminators = $analysis->getAnnotationsOfType(OA\Discriminator::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AugmentParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function setAugmentOperationParameters(bool $augmentOperationParameters):
return $this;
}

public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
$this->augmentSharedParameters($analysis);
if ($this->augmentOperationParameters) {
Expand Down
14 changes: 6 additions & 8 deletions src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AugmentProperties
use Concerns\RefTrait;
use Concerns\TypesTrait;

public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
/** @var OA\Property[] $properties */
$properties = $analysis->getAnnotationsOfType(OA\Property::class);
Expand Down Expand Up @@ -93,13 +93,11 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex
}
} elseif ($typeMatches[2] === '[]') {
if (Generator::isDefault($property->items)) {
$property->items = $items = new OA\Items(
[
'type' => $property->type,
'_context' => new Context(['generated' => true], $context),
]
);
$analysis->addAnnotation($items, $items->_context);
$property->items = new OA\Items([
'type' => $property->type,
'_context' => new Context(['generated' => true], $context),
]);
$analysis->addAnnotation($property->items, $property->items->_context);
if (!Generator::isDefault($property->ref)) {
$property->items->ref = $property->ref;
$property->ref = Generator::UNDEFINED;
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AugmentRefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AugmentRefs
{
use Concerns\RefTrait;

public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
$this->resolveAllOfRefs($analysis);
$this->resolveFQCNRefs($analysis);
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AugmentRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class AugmentRequestBody
{
public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
/** @var array<OA\RequestBody> $requests */
$requests = $analysis->getAnnotationsOfType(OA\RequestBody::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AugmentSchemas.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class AugmentSchemas
{
public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
/** @var OA\Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class);
Expand Down
4 changes: 2 additions & 2 deletions src/Processors/AugmentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setWhitelist(array $whitelist): AugmentTags
return $this;
}

public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
/** @var OA\Operation[] $operations */
$operations = $analysis->getAnnotationsOfType(OA\Operation::class);
Expand Down Expand Up @@ -70,7 +70,7 @@ public function __invoke(Analysis $analysis)
$this->removeUnusedTags($usedTagNames, $declaredTags, $analysis);
}

private function removeUnusedTags(array $usedTagNames, array $declaredTags, Analysis $analysis)
private function removeUnusedTags(array $usedTagNames, array $declaredTags, Analysis $analysis): void
{
if (in_array('*', $this->whitelist)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/BuildPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class BuildPaths
{
public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
$paths = [];
// Merge @OA\PathItems with the same path.
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/CleanUnmerged.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class CleanUnmerged
{
public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
$split = $analysis->split();
$merged = $split->merged->annotations;
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/CleanUnusedComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setEnabled(bool $enabled): CleanUnusedComponents
return $this;
}

public function __invoke(Analysis $analysis)
public function __invoke(Analysis $analysis): void
{
if (!$this->enabled || Generator::isDefault($analysis->openapi->components)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Processors/Concerns/AnnotationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function collectAnnotations($root): \SplObjectStorage
{
$storage = new \SplObjectStorage();

$this->traverseAnnotations($root, function ($item) use (&$storage) {
$this->traverseAnnotations($root, function ($item) use (&$storage): void {
if ($item instanceof OA\AbstractAnnotation && !$storage->contains($item)) {
$storage->attach($item);
}
Expand All @@ -34,7 +34,7 @@ public function collectAnnotations($root): \SplObjectStorage
public function removeAnnotation(iterable $root, OA\AbstractAnnotation $annotation, bool $recurse = true): void
{
$remove = $this->collectAnnotations($annotation);
$this->traverseAnnotations($root, function ($item) use ($remove) {
$this->traverseAnnotations($root, function ($item) use ($remove): void {
if ($item instanceof \SplObjectStorage) {
foreach ($remove as $annotation) {
$item->detach($annotation);
Expand Down
4 changes: 2 additions & 2 deletions src/Processors/Concerns/DocblockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function extractVarTypeAndDescription(?string $docblock): array

return array_merge(
['type' => null, 'description' => null],
array_filter($matches, fn ($key) => in_array($key, ['type', 'description']), ARRAY_FILTER_USE_KEY)
array_filter($matches, fn ($key): bool => in_array($key, ['type', 'description']), ARRAY_FILTER_USE_KEY)
);
}

Expand All @@ -193,7 +193,7 @@ public function extractExampleDescription(?string $docblock): ?string
{
preg_match('/@example\s+([ \t])?(?<example>.+)?$/im', $docblock, $matches);

return isset($matches['example']) ? $matches['example'] : null;
return $matches['example'] ?? null;
}

/**
Expand Down
Loading