Skip to content

Update to latest PHP 8.1 syntax #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
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
43 changes: 0 additions & 43 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,6 @@
<code>DocBlockGenerator::fromArray($value)</code>
<code>ParameterGenerator::fromArray($parameter)</code>
</DeprecatedMethod>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MixedArgument>
<code><![CDATA[$array['name']]]></code>
<code>$value</code>
Expand Down Expand Up @@ -603,9 +600,6 @@
<code>$constants instanceof SplArrayObject || $constants instanceof StdlibArrayObject</code>
<code>$constants instanceof StdlibArrayObject</code>
</DocblockTypeContradiction>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MissingReturnType>
<code>initEnvironmentConstants</code>
</MissingReturnType>
Expand Down Expand Up @@ -678,9 +672,6 @@
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
</MethodSignatureMismatch>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MissingImmutableAnnotation>
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
Expand All @@ -705,55 +696,29 @@
<code>getStartLine</code>
</PossiblyFalseReference>
</file>
<file src="src/Reflection/DocBlock/Tag/AuthorTag.php">
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
</file>
<file src="src/Reflection/DocBlock/Tag/GenericTag.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->contentSplitCharacter]]></code>
</ArgumentTypeCoercion>
<ImplementedReturnTypeMismatch>
<code>string|null</code>
</ImplementedReturnTypeMismatch>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<PossiblyNullOperand>
<code><![CDATA[$this->name]]></code>
</PossiblyNullOperand>
<PossiblyUnusedMethod>
<code>getContent</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Reflection/DocBlock/Tag/LicenseTag.php">
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
</file>
<file src="src/Reflection/DocBlock/Tag/MethodTag.php">
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
</file>
<file src="src/Reflection/DocBlock/Tag/ParamTag.php">
<PossiblyUnusedMethod>
<code>getDescription</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Reflection/DocBlock/Tag/PropertyTag.php">
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
</file>
<file src="src/Reflection/DocBlockReflection.php">
<DocblockTypeContradiction>
<code>! is_string($filter)</code>
</DocblockTypeContradiction>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MixedArgument>
<code><![CDATA[$tag['name']]]></code>
<code><![CDATA[$tag['value']]]></code>
Expand Down Expand Up @@ -831,15 +796,10 @@
<MethodSignatureMismatch>
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
<code>public function __toString()</code>
</MethodSignatureMismatch>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MissingImmutableAnnotation>
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
<code>public function __toString()</code>
</MissingImmutableAnnotation>
<MixedArgument>
<code>$returnTypes</code>
Expand Down Expand Up @@ -897,9 +857,6 @@
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
</MethodSignatureMismatch>
<MethodSignatureMustProvideReturnType>
<code>__toString</code>
</MethodSignatureMustProvideReturnType>
<MissingImmutableAnnotation>
<code>#[ReturnTypeWillChange]</code>
<code>#[ReturnTypeWillChange]</code>
Expand Down
9 changes: 1 addition & 8 deletions src/DeclareStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ class DeclareStatement
self::ENCODING => 'string',
];

protected string $directive;

/** @var int|string */
protected $value;

/** @param int|string $value */
private function __construct(string $directive, $value)
private function __construct(protected string $directive, protected $value)
{
$this->directive = $directive;
$this->value = $value;
}

public function getDirective(): string
Expand Down
6 changes: 1 addition & 5 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,7 @@ public function removeImplementedInterface($implementedInterface)
*/
public function getConstant($constantName)
{
if (isset($this->constants[$constantName])) {
return $this->constants[$constantName];
}

return false;
return $this->constants[$constantName] ?? false;
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/Generator/EnumGenerator/EnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,11 @@ final class EnumGenerator
*/
private const INDENTATION = ' ';

private Name $name;

/** @var BackedCases|PureCases */
private $cases;

/**
* @param BackedCases|PureCases $cases
*/
private function __construct(Name $name, $cases)
private function __construct(private readonly Name $name, private $cases)
{
$this->name = $name;
$this->cases = $cases;
}

public function generate(): string
Expand Down
8 changes: 1 addition & 7 deletions src/Generator/EnumGenerator/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@
*/
final class Name
{
private string $name;

private ?string $namespace;

private function __construct(string $name, ?string $namespace)
private function __construct(private readonly string $name, private readonly ?string $namespace)
{
$this->name = $name;
$this->namespace = $namespace;
}

public function getName(): string
Expand Down
9 changes: 5 additions & 4 deletions src/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laminas\Code\Generator;

use Laminas\Code\Reflection\MethodReflection;
use Stringable;

use function array_map;
use function explode;
Expand All @@ -12,13 +13,14 @@
use function preg_replace;
use function sprintf;
use function str_replace;
use function str_starts_with;
use function strlen;
use function strtolower;
use function substr;
use function trim;
use function uasort;

class MethodGenerator extends AbstractMemberGenerator
class MethodGenerator extends AbstractMemberGenerator implements Stringable
{
protected ?DocBlockGenerator $docBlock = null;

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

foreach ($lines as $key => $line) {
if (substr($line, 0, strlen($indention)) == $indention) {
if (str_starts_with($line, $indention)) {
$lines[$key] = substr($line, strlen($indention));
}
}
Expand Down Expand Up @@ -397,8 +399,7 @@ public function generate()
return $output;
}

/** @return string */
public function __toString()
public function __toString(): string
{
return $this->generate();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
try {
$param->setDefaultValue($reflectionParameter->getDefaultValue());
} catch (ReflectionException $e) {
} catch (ReflectionException) {
$param->setDefaultValue(null);
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/Generator/PromotedParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ final class PromotedParameterGenerator extends ParameterGenerator
public const VISIBILITY_PROTECTED = 'protected';
public const VISIBILITY_PRIVATE = 'private';

/** @psalm-var PromotedParameterGenerator::VISIBILITY_* */
private string $visibility;

/**
* @psalm-param non-empty-string $name
* @psalm-param ?non-empty-string $type
Expand All @@ -26,7 +23,7 @@ final class PromotedParameterGenerator extends ParameterGenerator
public function __construct(
string $name,
?string $type = null,
string $visibility = self::VISIBILITY_PUBLIC,
private readonly string $visibility = self::VISIBILITY_PUBLIC,
?int $position = null,
bool $passByReference = false
) {
Expand All @@ -37,8 +34,6 @@ public function __construct(
$position,
$passByReference,
);

$this->visibility = $visibility;
}

/** @psalm-return non-empty-string */
Expand Down
10 changes: 2 additions & 8 deletions src/Generator/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class PropertyGenerator extends AbstractMemberGenerator

protected bool $isConst = false;

protected ?TypeGenerator $type = null;

protected ?PropertyValueGenerator $defaultValue = null;

private bool $omitDefaultValue = false;
Expand All @@ -32,7 +30,7 @@ public function __construct(
?string $name = null,
$defaultValue = null,
$flags = self::FLAG_PUBLIC,
?TypeGenerator $type = null
protected ?TypeGenerator $type = null
) {
parent::__construct();

Expand All @@ -45,8 +43,6 @@ public function __construct(
if ($flags !== self::FLAG_PUBLIC) {
$this->setFlags($flags);
}

$this->type = $type;
}

/** @return static */
Expand Down Expand Up @@ -232,9 +228,7 @@ public function isReadonly(): bool
/** @inheritDoc */
public function setFlags($flags)
{
$flags = array_reduce((array) $flags, static function (int $a, int $b): int {
return $a | $b;
}, 0);
$flags = array_reduce((array) $flags, static fn(int $a, int $b): int => $a | $b, 0);

if ($flags & self::FLAG_READONLY && $flags & self::FLAG_STATIC) {
throw new Exception\RuntimeException('Modifier "readonly" in combination with "static" not permitted.');
Expand Down
5 changes: 1 addition & 4 deletions src/Generator/TraitUsageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
/** @psalm-type Visibility = ReflectionMethod::IS_PRIVATE|ReflectionMethod::IS_PROTECTED|ReflectionMethod::IS_PUBLIC */
class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterface
{
protected ClassGenerator $classGenerator;

/** @psalm-var array<int, string> Array of trait names */
protected array $traits = [];
/**
Expand All @@ -43,9 +41,8 @@ class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterfa
/** @var array<non-empty-string, non-empty-string> Array of string names */
protected array $uses = [];

public function __construct(ClassGenerator $classGenerator)
public function __construct(protected ClassGenerator $classGenerator)
{
$this->classGenerator = $classGenerator;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionUnionType;
use Stringable;

use function array_map;
use function sprintf;
Expand All @@ -21,7 +22,7 @@
use function substr;

/** @psalm-immutable */
final class TypeGenerator implements GeneratorInterface
final class TypeGenerator implements GeneratorInterface, Stringable
{
private const NULL_MARKER = '?';

Expand Down
14 changes: 4 additions & 10 deletions src/Generator/TypeGenerator/AtomicType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,14 @@ final class AtomicType
private const VALID_IDENTIFIER_MATCHER = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'
. '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/';

/** @psalm-var value-of<AtomicType::BUILT_IN_TYPES_PRECEDENCE>|0 */
public int $sortIndex;

/** @psalm-var non-empty-string */
public string $type;

/**
* @psalm-param non-empty-string $type
* @psalm-param value-of<AtomicType::BUILT_IN_TYPES_PRECEDENCE>|0 $sortIndex
*/
private function __construct(string $type, int $sortIndex)
{
$this->type = $type;
$this->sortIndex = $sortIndex;
private function __construct(
public string $type,
public int $sortIndex
) {
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayObject as SplArrayObject;
use Laminas\Code\Exception\InvalidArgumentException;
use Laminas\Stdlib\ArrayObject as StdlibArrayObject;
use Stringable;
use UnitEnum;

use function addcslashes;
Expand All @@ -25,7 +26,7 @@
use function str_contains;
use function str_repeat;

class ValueGenerator extends AbstractGenerator
class ValueGenerator extends AbstractGenerator implements Stringable
{
/**#@+
* Constant values
Expand Down Expand Up @@ -500,8 +501,7 @@ public function getOutputMode()
return $this->outputMode;
}

/** @return string */
public function __toString()
public function __toString(): string
{
return $this->generate();
}
Expand Down
5 changes: 1 addition & 4 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ public function toString()
return parent::__toString();
}

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return parent::__toString();
}
Expand Down
Loading