Skip to content
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

Migrate codebase to PHP 8.1 #94

Merged
merged 1 commit into from
Dec 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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),
);
}
}
31 changes: 12 additions & 19 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,8 +22,6 @@
use function strlen;
use function unserialize;

use const PHP_VERSION_ID;

final class Instantiator implements InstantiatorInterface
{
/**
Expand All @@ -31,37 +31,33 @@ final class Instantiator implements InstantiatorInterface
*
* @deprecated This constant will be private in 2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to remove that @deprecated tag

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Wanna submit a PR?

*/
public const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C';

/** @deprecated This constant will be private in 2.0 */
public const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';
private const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C';
private const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';
Comment on lines +34 to +35
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those constants are never used publicly and I believe they should not be part of the public interface. Do we need to flag them as deprecated on 1.x or is it enough to just switch them to private in 2.0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's flag them as deprecated out of consistency with doctrine/orm#10342?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96


/**
* 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 @@ -84,12 +80,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 @@ -127,14 +122,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 @@ -153,7 +146,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 @@ -181,7 +174,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