Skip to content

Commit 1793e78

Browse files
authored
Merge pull request #204 from samsonasik/update-to-php81-syntax
Update to latest PHP 8.1 syntax
2 parents c3d187b + 6f246fc commit 1793e78

25 files changed

+64
-155
lines changed

psalm-baseline.xml

-43
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,6 @@
373373
<code>DocBlockGenerator::fromArray($value)</code>
374374
<code>ParameterGenerator::fromArray($parameter)</code>
375375
</DeprecatedMethod>
376-
<MethodSignatureMustProvideReturnType>
377-
<code>__toString</code>
378-
</MethodSignatureMustProvideReturnType>
379376
<MixedArgument>
380377
<code><![CDATA[$array['name']]]></code>
381378
<code>$value</code>
@@ -603,9 +600,6 @@
603600
<code>$constants instanceof SplArrayObject || $constants instanceof StdlibArrayObject</code>
604601
<code>$constants instanceof StdlibArrayObject</code>
605602
</DocblockTypeContradiction>
606-
<MethodSignatureMustProvideReturnType>
607-
<code>__toString</code>
608-
</MethodSignatureMustProvideReturnType>
609603
<MissingReturnType>
610604
<code>initEnvironmentConstants</code>
611605
</MissingReturnType>
@@ -678,9 +672,6 @@
678672
<code>#[ReturnTypeWillChange]</code>
679673
<code>#[ReturnTypeWillChange]</code>
680674
</MethodSignatureMismatch>
681-
<MethodSignatureMustProvideReturnType>
682-
<code>__toString</code>
683-
</MethodSignatureMustProvideReturnType>
684675
<MissingImmutableAnnotation>
685676
<code>#[ReturnTypeWillChange]</code>
686677
<code>#[ReturnTypeWillChange]</code>
@@ -705,55 +696,29 @@
705696
<code>getStartLine</code>
706697
</PossiblyFalseReference>
707698
</file>
708-
<file src="src/Reflection/DocBlock/Tag/AuthorTag.php">
709-
<MethodSignatureMustProvideReturnType>
710-
<code>__toString</code>
711-
</MethodSignatureMustProvideReturnType>
712-
</file>
713699
<file src="src/Reflection/DocBlock/Tag/GenericTag.php">
714700
<ArgumentTypeCoercion>
715701
<code><![CDATA[$this->contentSplitCharacter]]></code>
716702
</ArgumentTypeCoercion>
717703
<ImplementedReturnTypeMismatch>
718704
<code>string|null</code>
719705
</ImplementedReturnTypeMismatch>
720-
<MethodSignatureMustProvideReturnType>
721-
<code>__toString</code>
722-
</MethodSignatureMustProvideReturnType>
723706
<PossiblyNullOperand>
724707
<code><![CDATA[$this->name]]></code>
725708
</PossiblyNullOperand>
726709
<PossiblyUnusedMethod>
727710
<code>getContent</code>
728711
</PossiblyUnusedMethod>
729712
</file>
730-
<file src="src/Reflection/DocBlock/Tag/LicenseTag.php">
731-
<MethodSignatureMustProvideReturnType>
732-
<code>__toString</code>
733-
</MethodSignatureMustProvideReturnType>
734-
</file>
735-
<file src="src/Reflection/DocBlock/Tag/MethodTag.php">
736-
<MethodSignatureMustProvideReturnType>
737-
<code>__toString</code>
738-
</MethodSignatureMustProvideReturnType>
739-
</file>
740713
<file src="src/Reflection/DocBlock/Tag/ParamTag.php">
741714
<PossiblyUnusedMethod>
742715
<code>getDescription</code>
743716
</PossiblyUnusedMethod>
744717
</file>
745-
<file src="src/Reflection/DocBlock/Tag/PropertyTag.php">
746-
<MethodSignatureMustProvideReturnType>
747-
<code>__toString</code>
748-
</MethodSignatureMustProvideReturnType>
749-
</file>
750718
<file src="src/Reflection/DocBlockReflection.php">
751719
<DocblockTypeContradiction>
752720
<code>! is_string($filter)</code>
753721
</DocblockTypeContradiction>
754-
<MethodSignatureMustProvideReturnType>
755-
<code>__toString</code>
756-
</MethodSignatureMustProvideReturnType>
757722
<MixedArgument>
758723
<code><![CDATA[$tag['name']]]></code>
759724
<code><![CDATA[$tag['value']]]></code>
@@ -831,15 +796,10 @@
831796
<MethodSignatureMismatch>
832797
<code>#[ReturnTypeWillChange]</code>
833798
<code>#[ReturnTypeWillChange]</code>
834-
<code>public function __toString()</code>
835799
</MethodSignatureMismatch>
836-
<MethodSignatureMustProvideReturnType>
837-
<code>__toString</code>
838-
</MethodSignatureMustProvideReturnType>
839800
<MissingImmutableAnnotation>
840801
<code>#[ReturnTypeWillChange]</code>
841802
<code>#[ReturnTypeWillChange]</code>
842-
<code>public function __toString()</code>
843803
</MissingImmutableAnnotation>
844804
<MixedArgument>
845805
<code>$returnTypes</code>
@@ -897,9 +857,6 @@
897857
<code>#[ReturnTypeWillChange]</code>
898858
<code>#[ReturnTypeWillChange]</code>
899859
</MethodSignatureMismatch>
900-
<MethodSignatureMustProvideReturnType>
901-
<code>__toString</code>
902-
</MethodSignatureMustProvideReturnType>
903860
<MissingImmutableAnnotation>
904861
<code>#[ReturnTypeWillChange]</code>
905862
<code>#[ReturnTypeWillChange]</code>

src/DeclareStatement.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@ class DeclareStatement
2626
self::ENCODING => 'string',
2727
];
2828

29-
protected string $directive;
30-
31-
/** @var int|string */
32-
protected $value;
33-
3429
/** @param int|string $value */
35-
private function __construct(string $directive, $value)
30+
private function __construct(protected string $directive, protected $value)
3631
{
37-
$this->directive = $directive;
38-
$this->value = $value;
3932
}
4033

4134
public function getDirective(): string

src/Generator/ClassGenerator.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,7 @@ public function removeImplementedInterface($implementedInterface)
532532
*/
533533
public function getConstant($constantName)
534534
{
535-
if (isset($this->constants[$constantName])) {
536-
return $this->constants[$constantName];
537-
}
538-
539-
return false;
535+
return $this->constants[$constantName] ?? false;
540536
}
541537

542538
/**

src/Generator/EnumGenerator/EnumGenerator.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,11 @@ final class EnumGenerator
2323
*/
2424
private const INDENTATION = ' ';
2525

26-
private Name $name;
27-
28-
/** @var BackedCases|PureCases */
29-
private $cases;
30-
3126
/**
3227
* @param BackedCases|PureCases $cases
3328
*/
34-
private function __construct(Name $name, $cases)
29+
private function __construct(private readonly Name $name, private $cases)
3530
{
36-
$this->name = $name;
37-
$this->cases = $cases;
3831
}
3932

4033
public function generate(): string

src/Generator/EnumGenerator/Name.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@
1212
*/
1313
final class Name
1414
{
15-
private string $name;
16-
17-
private ?string $namespace;
18-
19-
private function __construct(string $name, ?string $namespace)
15+
private function __construct(private readonly string $name, private readonly ?string $namespace)
2016
{
21-
$this->name = $name;
22-
$this->namespace = $namespace;
2317
}
2418

2519
public function getName(): string

src/Generator/MethodGenerator.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laminas\Code\Generator;
44

55
use Laminas\Code\Reflection\MethodReflection;
6+
use Stringable;
67

78
use function array_map;
89
use function explode;
@@ -12,13 +13,14 @@
1213
use function preg_replace;
1314
use function sprintf;
1415
use function str_replace;
16+
use function str_starts_with;
1517
use function strlen;
1618
use function strtolower;
1719
use function substr;
1820
use function trim;
1921
use function uasort;
2022

21-
class MethodGenerator extends AbstractMemberGenerator
23+
class MethodGenerator extends AbstractMemberGenerator implements Stringable
2224
{
2325
protected ?DocBlockGenerator $docBlock = null;
2426

@@ -106,7 +108,7 @@ protected static function clearBodyIndention($body)
106108
$indention = str_replace(trim($lines[1]), '', $lines[1]);
107109

108110
foreach ($lines as $key => $line) {
109-
if (substr($line, 0, strlen($indention)) == $indention) {
111+
if (str_starts_with($line, $indention)) {
110112
$lines[$key] = substr($line, strlen($indention));
111113
}
112114
}
@@ -397,8 +399,7 @@ public function generate()
397399
return $output;
398400
}
399401

400-
/** @return string */
401-
public function __toString()
402+
public function __toString(): string
402403
{
403404
return $this->generate();
404405
}

src/Generator/ParameterGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
4646
if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
4747
try {
4848
$param->setDefaultValue($reflectionParameter->getDefaultValue());
49-
} catch (ReflectionException $e) {
49+
} catch (ReflectionException) {
5050
$param->setDefaultValue(null);
5151
}
5252
}

src/Generator/PromotedParameterGenerator.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ final class PromotedParameterGenerator extends ParameterGenerator
1515
public const VISIBILITY_PROTECTED = 'protected';
1616
public const VISIBILITY_PRIVATE = 'private';
1717

18-
/** @psalm-var PromotedParameterGenerator::VISIBILITY_* */
19-
private string $visibility;
20-
2118
/**
2219
* @psalm-param non-empty-string $name
2320
* @psalm-param ?non-empty-string $type
@@ -26,7 +23,7 @@ final class PromotedParameterGenerator extends ParameterGenerator
2623
public function __construct(
2724
string $name,
2825
?string $type = null,
29-
string $visibility = self::VISIBILITY_PUBLIC,
26+
private readonly string $visibility = self::VISIBILITY_PUBLIC,
3027
?int $position = null,
3128
bool $passByReference = false
3229
) {
@@ -37,8 +34,6 @@ public function __construct(
3734
$position,
3835
$passByReference,
3936
);
40-
41-
$this->visibility = $visibility;
4237
}
4338

4439
/** @psalm-return non-empty-string */

src/Generator/PropertyGenerator.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class PropertyGenerator extends AbstractMemberGenerator
1818

1919
protected bool $isConst = false;
2020

21-
protected ?TypeGenerator $type = null;
22-
2321
protected ?PropertyValueGenerator $defaultValue = null;
2422

2523
private bool $omitDefaultValue = false;
@@ -32,7 +30,7 @@ public function __construct(
3230
?string $name = null,
3331
$defaultValue = null,
3432
$flags = self::FLAG_PUBLIC,
35-
?TypeGenerator $type = null
33+
protected ?TypeGenerator $type = null
3634
) {
3735
parent::__construct();
3836

@@ -45,8 +43,6 @@ public function __construct(
4543
if ($flags !== self::FLAG_PUBLIC) {
4644
$this->setFlags($flags);
4745
}
48-
49-
$this->type = $type;
5046
}
5147

5248
/** @return static */
@@ -232,9 +228,7 @@ public function isReadonly(): bool
232228
/** @inheritDoc */
233229
public function setFlags($flags)
234230
{
235-
$flags = array_reduce((array) $flags, static function (int $a, int $b): int {
236-
return $a | $b;
237-
}, 0);
231+
$flags = array_reduce((array) $flags, static fn(int $a, int $b): int => $a | $b, 0);
238232

239233
if ($flags & self::FLAG_READONLY && $flags & self::FLAG_STATIC) {
240234
throw new Exception\RuntimeException('Modifier "readonly" in combination with "static" not permitted.');

src/Generator/TraitUsageGenerator.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
/** @psalm-type Visibility = ReflectionMethod::IS_PRIVATE|ReflectionMethod::IS_PROTECTED|ReflectionMethod::IS_PUBLIC */
2323
class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterface
2424
{
25-
protected ClassGenerator $classGenerator;
26-
2725
/** @psalm-var array<int, string> Array of trait names */
2826
protected array $traits = [];
2927
/**
@@ -43,9 +41,8 @@ class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterfa
4341
/** @var array<non-empty-string, non-empty-string> Array of string names */
4442
protected array $uses = [];
4543

46-
public function __construct(ClassGenerator $classGenerator)
44+
public function __construct(protected ClassGenerator $classGenerator)
4745
{
48-
$this->classGenerator = $classGenerator;
4946
}
5047

5148
/**

src/Generator/TypeGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ReflectionIntersectionType;
1414
use ReflectionNamedType;
1515
use ReflectionUnionType;
16+
use Stringable;
1617

1718
use function array_map;
1819
use function sprintf;
@@ -21,7 +22,7 @@
2122
use function substr;
2223

2324
/** @psalm-immutable */
24-
final class TypeGenerator implements GeneratorInterface
25+
final class TypeGenerator implements GeneratorInterface, Stringable
2526
{
2627
private const NULL_MARKER = '?';
2728

src/Generator/TypeGenerator/AtomicType.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,14 @@ final class AtomicType
6262
private const VALID_IDENTIFIER_MATCHER = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'
6363
. '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/';
6464

65-
/** @psalm-var value-of<AtomicType::BUILT_IN_TYPES_PRECEDENCE>|0 */
66-
public int $sortIndex;
67-
68-
/** @psalm-var non-empty-string */
69-
public string $type;
70-
7165
/**
7266
* @psalm-param non-empty-string $type
7367
* @psalm-param value-of<AtomicType::BUILT_IN_TYPES_PRECEDENCE>|0 $sortIndex
7468
*/
75-
private function __construct(string $type, int $sortIndex)
76-
{
77-
$this->type = $type;
78-
$this->sortIndex = $sortIndex;
69+
private function __construct(
70+
public string $type,
71+
public int $sortIndex
72+
) {
7973
}
8074

8175
/**

src/Generator/ValueGenerator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArrayObject as SplArrayObject;
66
use Laminas\Code\Exception\InvalidArgumentException;
77
use Laminas\Stdlib\ArrayObject as StdlibArrayObject;
8+
use Stringable;
89
use UnitEnum;
910

1011
use function addcslashes;
@@ -25,7 +26,7 @@
2526
use function str_contains;
2627
use function str_repeat;
2728

28-
class ValueGenerator extends AbstractGenerator
29+
class ValueGenerator extends AbstractGenerator implements Stringable
2930
{
3031
/**#@+
3132
* Constant values
@@ -500,8 +501,7 @@ public function getOutputMode()
500501
return $this->outputMode;
501502
}
502503

503-
/** @return string */
504-
public function __toString()
504+
public function __toString(): string
505505
{
506506
return $this->generate();
507507
}

src/Reflection/ClassReflection.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,7 @@ public function toString()
202202
return parent::__toString();
203203
}
204204

205-
/**
206-
* @return string
207-
*/
208-
public function __toString()
205+
public function __toString(): string
209206
{
210207
return parent::__toString();
211208
}

0 commit comments

Comments
 (0)