Skip to content

Commit

Permalink
Migrate codebase to PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Dec 29, 2022
1 parent 6f1e0c4 commit e4a3e60
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/continuous-integration.yml@3.0.0"
with:
php-versions: '["7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]'
php-versions: '["8.1", "8.2"]'
composer-root-version: "1.4"
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
}
],
"require": {
"php": "^7.1 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"ext-phar": "*",
"ext-pdo": "*",
"doctrine/coding-standard": "^9 || ^11",
"phpbench/phpbench": "^0.16 || ^1",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
"vimeo/psalm": "^4.30 || ^5.4"
"doctrine/coding-standard": "^11",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.9.4",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5.27",
"vimeo/psalm": "^5.4"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 1 addition & 30 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,15 @@
<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<config name="php_version" value="70100"/>
<config name="php_version" value="80100"/>

<file>src</file>
<file>tests</file>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException"/>
</rule>

<!-- Disable the rules that will require PHP 7.4 -->
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<properties>
<property name="enableNativeTypeHint" value="false"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
<exclude-pattern>src/*</exclude-pattern>
<exclude-pattern>tests/DoctrineTest/InstantiatorTestAsset/SerializableArrayObjectAsset.php</exclude-pattern>
<exclude-pattern>tests/DoctrineTest/InstantiatorTestAsset/SimpleSerializableAsset.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
<exclude-pattern>*/src/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming">
<exclude-pattern>tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php</exclude-pattern>
</rule>
Expand All @@ -51,13 +31,4 @@
<exclude-pattern>src/Doctrine/Instantiator/Exception/ExceptionInterface.php</exclude-pattern>
<exclude-pattern>src/Doctrine/Instantiator/InstantiatorInterface.php</exclude-pattern>
</rule>

<rule ref="Generic.WhiteSpace.ScopeIndent.Incorrect">
<!-- see https://github.com/squizlabs/PHP_CodeSniffer/issues/3474 -->
<exclude-pattern>tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php</exclude-pattern>
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent.IncorrectExact">
<!-- see https://github.com/squizlabs/PHP_CodeSniffer/issues/3474 -->
<exclude-pattern>tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php</exclude-pattern>
</rule>
</ruleset>
2 changes: 2 additions & 0 deletions src/Doctrine/Instantiator/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Instantiator\Exception;

use Throwable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Instantiator\Exception;

use InvalidArgumentException as BaseInvalidArgumentException;
Expand Down Expand Up @@ -36,15 +38,15 @@ public static function fromAbstractClass(ReflectionClass $reflectionClass): self
{
return new self(sprintf(
'The provided class "%s" is abstract, and cannot be instantiated',
$reflectionClass->getName()
$reflectionClass->getName(),
));
}

public static function fromEnum(string $className): self
{
return new self(sprintf(
'The provided class "%s" is an enum, and cannot be instantiated',
$className
$className,
));
}
}
14 changes: 8 additions & 6 deletions src/Doctrine/Instantiator/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Instantiator\Exception;

use Exception;
Expand All @@ -20,15 +22,15 @@ class UnexpectedValueException extends BaseUnexpectedValueException implements E
*/
public static function fromSerializationTriggeredException(
ReflectionClass $reflectionClass,
Exception $exception
Exception $exception,
): self {
return new self(
sprintf(
'An exception was raised while trying to instantiate an instance of "%s" via un-serialization',
$reflectionClass->getName()
$reflectionClass->getName(),
),
0,
$exception
$exception,
);
}

Expand All @@ -42,18 +44,18 @@ public static function fromUncleanUnSerialization(
string $errorString,
int $errorCode,
string $errorFile,
int $errorLine
int $errorLine,
): self {
return new self(
sprintf(
'Could not produce an instance of "%s" via un-serialization, since an error was triggered '
. 'in file "%s" at line "%d"',
$reflectionClass->getName(),
$errorFile,
$errorLine
$errorLine,
),
0,
new Exception($errorString, $errorCode)
new Exception($errorString, $errorCode),
);
}
}
29 changes: 12 additions & 17 deletions src/Doctrine/Instantiator/Instantiator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Instantiator;

use ArrayIterator;
Expand All @@ -20,44 +22,40 @@
use function strlen;
use function unserialize;

use const PHP_VERSION_ID;

final class Instantiator implements InstantiatorInterface
{
/**
* Markers used internally by PHP to define whether {@see \unserialize} should invoke
* the method {@see \Serializable::unserialize()} when dealing with classes implementing
* the {@see \Serializable} interface.
*/
public const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C';
public const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';
private const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C';
private const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';

/**
* Used to instantiate specific classes, indexed by class name.
*
* @var callable[]
*/
private static $cachedInstantiators = [];
private static array $cachedInstantiators = [];

/**
* Array of objects that can directly be cloned, indexed by class name.
*
* @var object[]
*/
private static $cachedCloneables = [];
private static array $cachedCloneables = [];

/**
* @param string $className
* @phpstan-param class-string<T> $className
*
* @return object
* @phpstan-return T
*
* @throws ExceptionInterface
*
* @template T of object
*/
public function instantiate($className)
public function instantiate(string $className): object
{
if (isset(self::$cachedCloneables[$className])) {
/** @phpstan-var T */
Expand All @@ -80,12 +78,11 @@ public function instantiate($className)
*
* @phpstan-param class-string<T> $className
*
* @return object
* @phpstan-return T
*
* @template T of object
*/
private function buildAndCacheFromFactory(string $className)
private function buildAndCacheFromFactory(string $className): object
{
$factory = self::$cachedInstantiators[$className] = $this->buildFactory($className);
$instance = $factory();
Expand Down Expand Up @@ -123,14 +120,12 @@ private function buildFactory(string $className): callable
'%s:%d:"%s":0:{}',
is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
strlen($className),
$className
$className,
);

$this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString);

return static function () use ($serializedString) {
return unserialize($serializedString);
};
return static fn () => unserialize($serializedString);
}

/**
Expand All @@ -149,7 +144,7 @@ private function getReflectionClass(string $className): ReflectionClass
throw InvalidArgumentException::fromNonExistingClass($className);
}

if (PHP_VERSION_ID >= 80100 && enum_exists($className, false)) {
if (enum_exists($className, false)) {
throw InvalidArgumentException::fromEnum($className);
}

Expand Down Expand Up @@ -177,7 +172,7 @@ private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionCl
$message,
$code,
$file,
$line
$line,
);

return true;
Expand Down
6 changes: 3 additions & 3 deletions src/Doctrine/Instantiator/InstantiatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Instantiator;

use Doctrine\Instantiator\Exception\ExceptionInterface;
Expand All @@ -10,15 +12,13 @@
interface InstantiatorInterface
{
/**
* @param string $className
* @phpstan-param class-string<T> $className
*
* @return object
* @phpstan-return T
*
* @throws ExceptionInterface
*
* @template T of object
*/
public function instantiate($className);
public function instantiate(string $className): object;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\InstantiatorPerformance;

use ArrayObject;
Expand All @@ -17,8 +19,7 @@
*/
class InstantiatorPerformanceBench
{
/** @var Instantiator */
private $instantiator;
private Instantiator $instantiator;

public function init(): void
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\InstantiatorTest\Exception;

use Doctrine\Instantiator\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -34,7 +36,7 @@ public function testFromNonExistingTypeWithTrait(): void

self::assertSame(
sprintf('The provided type "%s" is a trait, and cannot be instantiated', SimpleTraitAsset::class),
$exception->getMessage()
$exception->getMessage(),
);
}

Expand All @@ -45,9 +47,9 @@ public function testFromNonExistingTypeWithInterface(): void
self::assertSame(
sprintf(
'The provided type "%s" is an interface, and cannot be instantiated',
InstantiatorInterface::class
InstantiatorInterface::class,
),
$exception->getMessage()
$exception->getMessage(),
);
}

Expand All @@ -59,9 +61,9 @@ public function testFromAbstractClass(): void
self::assertSame(
sprintf(
'The provided class "%s" is abstract, and cannot be instantiated',
AbstractClassAsset::class
AbstractClassAsset::class,
),
$exception->getMessage()
$exception->getMessage(),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\InstantiatorTest\Exception;

use Doctrine\Instantiator\Exception\UnexpectedValueException;
Expand Down Expand Up @@ -27,7 +29,7 @@ public function testFromSerializationTriggeredException(): void
self::assertSame(
'An exception was raised while trying to instantiate an instance of "'
. self::class . '" via un-serialization',
$exception->getMessage()
$exception->getMessage(),
);
}

Expand All @@ -40,9 +42,9 @@ public function testFromUncleanUnSerialization(): void
sprintf(
'Could not produce an instance of "%s" '
. 'via un-serialization, since an error was triggered in file "bar" at line "456"',
AbstractClassAsset::class
AbstractClassAsset::class,
),
$exception->getMessage()
$exception->getMessage(),
);

$previous = $exception->getPrevious();
Expand Down
Loading

0 comments on commit e4a3e60

Please sign in to comment.