Skip to content

Commit 5f3f9c7

Browse files
committed
CS: Follow rector rule changes
1 parent e84de2d commit 5f3f9c7

17 files changed

Lines changed: 63 additions & 72 deletions

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
1212
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
1313
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
14+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
1415
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
1516
use Rector\ValueObject\PhpVersion;
1617

@@ -35,6 +36,7 @@
3536
ParamTypeByMethodCallTypeRector::class => [
3637
__DIR__ . '/src/Serializer.php',
3738
],
39+
ClassPropertyAssignToConstructorPromotionRector::class,
3840
])
3941
->withPreparedSets(
4042
deadCode: true,

src/Analysers/ComposerAutoloaderScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function scan(array $namespaces): array
2929
if ($autoloader = static::getComposerAutoloader()) {
3030
foreach (array_keys($autoloader->getClassMap()) as $unit) {
3131
foreach ($namespaces as $namespace) {
32-
if (0 === strpos($unit, $namespace)) {
32+
if (str_starts_with($unit, $namespace)) {
3333
$units[] = $unit;
3434
break;
3535
}

src/Analysers/DocBlockAnnotationFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ public function build(\Reflector $reflector, Context $context): array
4141
$aliases = $this->generator ? $this->generator->getAliases() : [];
4242

4343
if (method_exists($reflector, 'getShortName') && method_exists($reflector, 'getName')) {
44-
$aliases[strtolower($reflector->getShortName())] = $reflector->getName();
44+
$aliases[strtolower((string) $reflector->getShortName())] = $reflector->getName();
4545
}
4646

4747
if ($context->with('scanned')) {
4848
$details = $context->scanned;
4949
foreach ($details['uses'] as $alias => $name) {
50-
$aliasKey = strtolower($alias);
50+
$aliasKey = strtolower((string) $alias);
5151
if ($name != $alias && !array_key_exists($aliasKey, $aliases)) {
5252
// real aliases only
53-
$aliases[strtolower($alias)] = $name;
53+
$aliases[strtolower((string) $alias)] = $name;
5454
}
5555
}
5656
}

src/Analysers/TokenScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function collect_stmts(array $stmts, string $namespace): array
7171
};
7272
$result = [];
7373
foreach ($stmts as $stmt) {
74-
switch (get_class($stmt)) {
74+
switch ($stmt::class) {
7575
case Use_::class:
7676
$uses += $this->collect_uses($stmt);
7777
break;

src/Annotations/AbstractAnnotation.php

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function mergeProperties($object): void
238238
if (Generator::isDefault($value)) {
239239
continue;
240240
}
241-
$identity = method_exists($object, 'identity') ? $object->identity() : get_class($object);
241+
$identity = method_exists($object, 'identity') ? $object->identity() : $object::class;
242242
$context1 = $this->_context;
243243
$context2 = property_exists($object, '_context') ? $object->_context : 'unknown';
244244
if ($this->{$property} instanceof AbstractAnnotation) {
@@ -460,14 +460,13 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
460460
break;
461461
}
462462

463-
/** @var class-string<AbstractAnnotation> $class */
464-
$class = get_class($annotation);
463+
$class = $annotation::class;
465464
if ($details = $this->matchNested($annotation)) {
466465
$property = $details->value;
467466
if (is_array($property)) {
468-
$this->_context->logger->warning('Only one ' . static::shorten(get_class($annotation)) . '() allowed for ' . $this->identity() . ' multiple found, skipped: ' . $annotation->_context);
467+
$this->_context->logger->warning('Only one ' . static::shorten($annotation::class) . '() allowed for ' . $this->identity() . ' multiple found, skipped: ' . $annotation->_context);
469468
} else {
470-
$this->_context->logger->warning('Only one ' . static::shorten(get_class($annotation)) . '() allowed for ' . $this->identity() . " multiple found in:\n Using: " . $this->{$property}->_context . "\n Skipped: " . $annotation->_context);
469+
$this->_context->logger->warning('Only one ' . static::shorten($annotation::class) . '() allowed for ' . $this->identity() . " multiple found in:\n Using: " . $this->{$property}->_context . "\n Skipped: " . $annotation->_context);
471470
}
472471
} elseif ($annotation instanceof AbstractAnnotation) {
473472
$message = 'Unexpected ' . $annotation->identity();
@@ -505,7 +504,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
505504
}
506505

507506
if (property_exists($this, 'ref') && !Generator::isDefault($this->ref) && is_string($this->ref)) {
508-
if (substr($this->ref, 0, 2) === '#/' && $stack !== [] && $stack[0] instanceof OpenApi) {
507+
if (str_starts_with($this->ref, '#/') && $stack !== [] && $stack[0] instanceof OpenApi) {
509508
// Internal reference
510509
try {
511510
$stack[0]->ref($this->ref);
@@ -552,7 +551,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
552551
$this->_context->logger->warning($this->identity() . '->' . $property . ' "' . $value . '" is invalid, expecting "' . implode('", "', $type) . '" in ' . $this->_context);
553552
}
554553
} else {
555-
throw new OpenApiException('Invalid ' . get_class($this) . '::$_types[' . $property . ']');
554+
throw new OpenApiException('Invalid ' . static::class . '::$_types[' . $property . ']');
556555
}
557556
}
558557
$stack[] = $this;
@@ -611,7 +610,7 @@ private static function _validate($fields, array $stack, array $skip, string $ba
611610
*/
612611
public function identity(): string
613612
{
614-
$class = get_class($this);
613+
$class = static::class;
615614
$properties = [];
616615
/** @var class-string<AbstractAnnotation> $parent */
617616
foreach (static::$_parents as $parent) {
@@ -652,10 +651,10 @@ public function matchNested($other)
652651
*/
653652
public function getRoot(): string
654653
{
655-
$class = get_class($this);
654+
$class = static::class;
656655

657656
do {
658-
if (0 === strpos($class, 'OpenApi\\Annotations\\')) {
657+
if (str_starts_with($class, 'OpenApi\\Annotations\\')) {
659658
break;
660659
}
661660
} while ($class = get_parent_class($class));
@@ -670,7 +669,7 @@ public function getRoot(): string
670669
*/
671670
public function isRoot(string $rootClass): bool
672671
{
673-
return get_class($this) === $rootClass || $this->getRoot() === $rootClass;
672+
return static::class === $rootClass || $this->getRoot() === $rootClass;
674673
}
675674

676675
/**
@@ -686,7 +685,7 @@ protected function _identity(array $properties): string
686685
}
687686
}
688687

689-
return static::shorten(get_class($this)) . '(' . implode(',', $fields) . ')';
688+
return static::shorten(static::class) . '(' . implode(',', $fields) . ')';
690689
}
691690

692691
/**
@@ -697,7 +696,7 @@ protected function _identity(array $properties): string
697696
*/
698697
private function validateType(string $type, $value): bool
699698
{
700-
if (substr($type, 0, 1) === '[' && substr($type, -1) === ']') { // Array of a specified type?
699+
if (str_starts_with($type, '[') && str_ends_with($type, ']')) { // Array of a specified type?
701700
if ($this->validateType('array', $value) === false) {
702701
return false;
703702
}
@@ -737,24 +736,16 @@ private function validateDefaultTypes(string $type, $value): bool
737736
return false;
738737
}
739738

740-
switch ($type) {
741-
case 'string':
742-
return is_string($value);
743-
case 'boolean':
744-
return is_bool($value);
745-
case 'integer':
746-
return is_int($value);
747-
case 'number':
748-
return is_numeric($value);
749-
case 'object':
750-
return is_object($value);
751-
case 'array':
752-
return $this->validateArrayType($value);
753-
case 'scheme':
754-
return in_array($value, ['http', 'https', 'ws', 'wss'], true);
755-
default:
756-
throw new OpenApiException('Invalid type "' . $type . '"');
757-
}
739+
return match ($type) {
740+
'string' => is_string($value),
741+
'boolean' => is_bool($value),
742+
'integer' => is_int($value),
743+
'number' => is_numeric($value),
744+
'object' => is_object($value),
745+
'array' => $this->validateArrayType($value),
746+
'scheme' => in_array($value, ['http', 'https', 'ws', 'wss'], true),
747+
default => throw new OpenApiException('Invalid type "' . $type . '"'),
748+
};
758749
}
759750

760751
/**

src/Annotations/OpenApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function saveAs(string $filename, string $format = 'auto'): void
217217
*/
218218
public function ref(string $ref)
219219
{
220-
if (substr($ref, 0, 2) !== '#/') {
220+
if (!str_starts_with($ref, '#/')) {
221221
// @todo Add support for external (http) refs?
222222
throw new OpenApiException('Unsupported $ref "' . $ref . '", it should start with "#/"');
223223
}
@@ -245,7 +245,7 @@ private static function resolveRef(string $ref, string $resolved, $container, ar
245245

246246
if (is_object($container)) {
247247
// support use x-* in ref
248-
$xKey = strpos($property, 'x-') === 0 ? substr($property, 2) : null;
248+
$xKey = str_starts_with($property, 'x-') ? substr($property, 2) : null;
249249
if ($xKey) {
250250
if (!is_array($container->x) || !array_key_exists($xKey, $container->x)) {
251251
$xKey = null;

src/Context.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @property string|null $version The OpenAPI version in use
4545
*/
4646
#[\AllowDynamicProperties]
47-
class Context
47+
class Context implements \Stringable
4848
{
4949
/**
5050
* Prototypical inheritance for properties.
@@ -257,7 +257,7 @@ public function fullyQualifiedName(?string $source): string
257257
} elseif ($this->uses) {
258258
// Unqualified name (Foo)
259259
foreach ($this->uses as $alias => $aliasedNamespace) {
260-
if (strcasecmp($alias, $source) === 0) {
260+
if (strcasecmp((string) $alias, $source) === 0) {
261261
return '\\' . $aliasedNamespace;
262262
}
263263
}

src/Generator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected function normaliseConfig(array $config): array
158158
$normalised = [];
159159
foreach ($config as $key => $value) {
160160
if (is_numeric($key)) {
161-
$token = explode('=', $value);
161+
$token = explode('=', (string) $value);
162162
if (2 == count($token)) {
163163
// 'operationId.hash=false'
164164
[$key, $value] = $token;
@@ -169,10 +169,10 @@ protected function normaliseConfig(array $config): array
169169
$value = 'true' == $value;
170170
}
171171

172-
if ($isList = ('[]' === substr($key, -2))) {
173-
$key = substr($key, 0, -2);
172+
if ($isList = (str_ends_with((string) $key, '[]'))) {
173+
$key = substr((string) $key, 0, -2);
174174
}
175-
$token = explode('.', $key);
175+
$token = explode('.', (string) $key);
176176
if (2 == count($token)) {
177177
// 'operationId.hash' => false
178178
// namespaced / processor

src/Pipeline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function insert(callable $pipe, $matcher): Pipeline
7171
{
7272
if (is_string($matcher)) {
7373
$before = $matcher;
74-
$matcher = function (array $pipes) use ($before) {
74+
$matcher = function (array $pipes) use ($before): int|string|null {
7575
foreach ($pipes as $ii => $current) {
7676
if ($current instanceof $before) {
7777
return $ii;

src/Processors/AugmentProperties.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ protected function augmentSchemaType(Analysis $analysis, OA\Schema $schema, Cont
9393
}
9494
} elseif ($typeMatches[2] === '[]') {
9595
if (Generator::isDefault($schema->items)) {
96-
$schema->items = new OA\Items(
97-
[
96+
$schema->items = new OA\Items([
9897
'type' => $schema->type,
9998
'_context' => new Context(['generated' => true], $context),
100-
]
101-
);
99+
]);
102100
$analysis->addAnnotation($schema->items, $schema->items->_context);
103101
if (!Generator::isDefault($schema->ref)) {
104102
$schema->items->ref = $schema->ref;
@@ -138,7 +136,7 @@ protected function augmentSchemaType(Analysis $analysis, OA\Schema $schema, Cont
138136
if ($context->nullable === true && Generator::isDefault($schema->nullable)) {
139137
$schema->nullable = true;
140138
}
141-
$type = strtolower($context->type);
139+
$type = strtolower((string) $context->type);
142140
if (!$this->mapNativeType($schema, $type)) {
143141
$typeSchema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
144142
if (Generator::isDefault($schema->ref) && $typeSchema) {
@@ -168,7 +166,7 @@ protected function isNullable(string $typeDescription): bool
168166

169167
protected function stripNull(string $typeDescription): string
170168
{
171-
if (strpos($typeDescription, '|') === false) {
169+
if (!str_contains($typeDescription, '|')) {
172170
return $typeDescription;
173171
}
174172
$types = [];

0 commit comments

Comments
 (0)