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

Cleanup: old php versions compat cleanup #23

Merged
merged 26 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1d505fd
Removing code that deals with PHP <= 5.6
Ocramius Dec 3, 2015
2a27cfa
`isCloneable` always exists
Ocramius Dec 3, 2015
d02ccc1
Method simplification
Ocramius Dec 3, 2015
c452d79
Reducing number of method calls in `Instantiator`
Ocramius Dec 3, 2015
737557a
Requiring at least PHP 5.6 when dealing with the next version of this…
Ocramius Dec 3, 2015
1b3f781
Testing should happen on PHP 7 as well
Ocramius Dec 3, 2015
b5f1d8f
Removing checks for PHP version 5.3.3 and other affected weirdos
Ocramius Dec 3, 2015
759fa3b
Removing incorrect bash syntax
Ocramius Dec 3, 2015
989a6e9
Master is now `1.1.x-dev`
Ocramius Dec 3, 2015
c1dc0f2
Removing clutter from tests (php version checks)
Ocramius Dec 4, 2015
508a3f9
`::class` over string constants
Ocramius Dec 4, 2015
f9a5d29
`::class` over string constants
Ocramius Dec 4, 2015
90bea39
`::class` over string constants
Ocramius Dec 4, 2015
e5af279
Short array syntax
Ocramius Dec 4, 2015
dbad3d2
Correcting documentation usage - should reference class names via `::…
Ocramius Dec 4, 2015
d398260
#23 dropped PHP versions previous to 7.1
Ocramius Jul 22, 2017
916a37f
#23 generic cleanups via additions of PHP 7 hints
Ocramius Jul 22, 2017
8e36d83
#23 adding return and parameter hints to existing API
Ocramius Jul 22, 2017
874097c
#23 adding return hints wherever possible
Ocramius Jul 22, 2017
bb7f6bd
#23 simplifying travis setup
Ocramius Jul 22, 2017
a07bb2f
#23 dropping HHVM-specific travis installation
Ocramius Jul 22, 2017
fd844cd
#23 CS - fixing too long line
Ocramius Jul 22, 2017
bb028f7
#23 testing also against nightly PHP versions
Ocramius Jul 22, 2017
22cbe19
#23 bumping dependencies
Ocramius Jul 22, 2017
fdbaab5
#23 migrating to PHPUnit 6.x test format
Ocramius Jul 22, 2017
131dfc6
#23 YAML is a stupid config format anyway
Ocramius Jul 22, 2017
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
10 changes: 0 additions & 10 deletions .travis.install.sh

This file was deleted.

17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- hhvm
- nightly

before_script:
- ./.travis.install.sh
- if [ $TRAVIS_PHP_VERSION = '5.6' ]; then PHPUNIT_FLAGS="--coverage-clover coverage.clover"; else PHPUNIT_FLAGS=""; fi
- composer self-update
- composer update

script:
- ./vendor/bin/phpunit $PHPUNIT_FLAGS;
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/;
- php -n ./vendor/bin/athletic -p ./tests/DoctrineTest/InstantiatorPerformance/ -f GroupedFormatter;
- ./vendor/bin/phpunit --coverage-clover coverage.clover
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/
- php -n ./vendor/bin/athletic -p ./tests/DoctrineTest/InstantiatorPerformance/ -f GroupedFormatter

after_script:
- if [ $TRAVIS_PHP_VERSION = '5.6' ]; then wget https://scrutinizer-ci.com/ocular.phar; php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ itself:
```php
$instantiator = new \Doctrine\Instantiator\Instantiator();

$instance = $instantiator->instantiate(My\ClassName\Here::class);
$instance = $instantiator->instantiate(\My\ClassName\Here::class);
```

## Contributing
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
}
],
"require": {
"php": ">=5.3,<8.0-DEV"
"php": "^7.1"
},
"require-dev": {
"ext-phar": "*",
"ext-pdo": "*",
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.0",
"phpunit/phpunit": "^6.2.3",
"squizlabs/php_codesniffer": "^3.0.2",
"athletic/athletic": "~0.1.8"
},
"autoload": {
Expand All @@ -39,7 +39,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.1.x-dev"
}
}
}
14 changes: 2 additions & 12 deletions src/Doctrine/Instantiator/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
*/
class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface
{
/**
* @param string $className
*
* @return self
*/
public static function fromNonExistingClass($className)
public static function fromNonExistingClass(string $className) : self
{
if (interface_exists($className)) {
return new self(sprintf('The provided type "%s" is an interface, and can not be instantiated', $className));
Expand All @@ -47,12 +42,7 @@ public static function fromNonExistingClass($className)
return new self(sprintf('The provided class "%s" does not exist', $className));
}

/**
* @param ReflectionClass $reflectionClass
*
* @return self
*/
public static function fromAbstractClass(ReflectionClass $reflectionClass)
public static function fromAbstractClass(ReflectionClass $reflectionClass) : self
{
return new self(sprintf(
'The provided class "%s" is abstract, and can not be instantiated',
Expand Down
31 changes: 9 additions & 22 deletions src/Doctrine/Instantiator/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@
*/
class UnexpectedValueException extends BaseUnexpectedValueException implements ExceptionInterface
{
/**
* @param ReflectionClass $reflectionClass
* @param Exception $exception
*
* @return self
*/
public static function fromSerializationTriggeredException(ReflectionClass $reflectionClass, Exception $exception)
{
public static function fromSerializationTriggeredException(
ReflectionClass $reflectionClass,
Exception $exception
) : self {
return new self(
sprintf(
'An exception was raised while trying to instantiate an instance of "%s" via un-serialization',
Expand All @@ -48,22 +44,13 @@ public static function fromSerializationTriggeredException(ReflectionClass $refl
);
}

/**
* @param ReflectionClass $reflectionClass
* @param string $errorString
* @param int $errorCode
* @param string $errorFile
* @param int $errorLine
*
* @return UnexpectedValueException
*/
public static function fromUncleanUnSerialization(
ReflectionClass $reflectionClass,
$errorString,
$errorCode,
$errorFile,
$errorLine
) {
string $errorString,
int $errorCode,
string $errorFile,
int $errorLine
) : self {
return new self(
sprintf(
'Could not produce an instance of "%s" via un-serialization, since an error was triggered '
Expand Down
103 changes: 23 additions & 80 deletions src/Doctrine/Instantiator/Instantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace Doctrine\Instantiator;

use Closure;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
use Doctrine\Instantiator\Exception\UnexpectedValueException;
use Exception;
Expand All @@ -41,14 +40,14 @@ final class Instantiator implements InstantiatorInterface
const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';

/**
* @var \Closure[] of {@see \Closure} instances used to instantiate specific classes
* @var \callable[] used to instantiate specific classes, indexed by class name
*/
private static $cachedInstantiators = array();
private static $cachedInstantiators = [];

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

/**
* {@inheritDoc}
Expand All @@ -71,11 +70,9 @@ public function instantiate($className)
/**
* Builds the requested object and caches it in static properties for performance
*
* @param string $className
*
* @return object
*/
private function buildAndCacheFromFactory($className)
private function buildAndCacheFromFactory(string $className)
{
$factory = self::$cachedInstantiators[$className] = $this->buildFactory($className);
$instance = $factory();
Expand All @@ -88,26 +85,24 @@ private function buildAndCacheFromFactory($className)
}

/**
* Builds a {@see \Closure} capable of instantiating the given $className without
* Builds a callable capable of instantiating the given $className without
* invoking its constructor.
*
* @param string $className
*
* @return Closure
* @throws InvalidArgumentException
* @throws UnexpectedValueException
* @throws \ReflectionException
*/
private function buildFactory($className)
private function buildFactory(string $className) : callable
{
$reflectionClass = $this->getReflectionClass($className);

if ($this->isInstantiableViaReflection($reflectionClass)) {
return function () use ($reflectionClass) {
return $reflectionClass->newInstanceWithoutConstructor();
};
return [$reflectionClass, 'newInstanceWithoutConstructor'];
}

$serializedString = sprintf(
'%s:%d:"%s":0:{}',
$this->getSerializationFormat($reflectionClass),
self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
strlen($className),
$className
);
Expand All @@ -125,8 +120,9 @@ private function buildFactory($className)
* @return ReflectionClass
*
* @throws InvalidArgumentException
* @throws \ReflectionException
*/
private function getReflectionClass($className)
private function getReflectionClass($className) : ReflectionClass
{
if (! class_exists($className)) {
throw InvalidArgumentException::fromNonExistingClass($className);
Expand All @@ -149,9 +145,9 @@ private function getReflectionClass($className)
*
* @return void
*/
private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString)
private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString) : void
{
set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) {
set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) : void {
$error = UnexpectedValueException::fromUncleanUnSerialization(
$reflectionClass,
$message,
Expand All @@ -178,7 +174,7 @@ private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionCl
*
* @return void
*/
private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString)
private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) : void
{
try {
unserialize($serializedString);
Expand All @@ -189,28 +185,15 @@ private function attemptInstantiationViaUnSerialization(ReflectionClass $reflect
}
}

/**
* @param ReflectionClass $reflectionClass
*
* @return bool
*/
private function isInstantiableViaReflection(ReflectionClass $reflectionClass)
private function isInstantiableViaReflection(ReflectionClass $reflectionClass) : bool
{
if (\PHP_VERSION_ID >= 50600) {
return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal());
}

return \PHP_VERSION_ID >= 50400 && ! $this->hasInternalAncestors($reflectionClass);
return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal());
}

/**
* Verifies whether the given class is to be considered internal
*
* @param ReflectionClass $reflectionClass
*
* @return bool
*/
private function hasInternalAncestors(ReflectionClass $reflectionClass)
private function hasInternalAncestors(ReflectionClass $reflectionClass) : bool
{
do {
if ($reflectionClass->isInternal()) {
Expand All @@ -221,53 +204,13 @@ private function hasInternalAncestors(ReflectionClass $reflectionClass)
return false;
}

/**
* Verifies if the given PHP version implements the `Serializable` interface serialization
* with an incompatible serialization format. If that's the case, use serialization marker
* "C" instead of "O".
*
* @link http://news.php.net/php.internals/74654
*
* @param ReflectionClass $reflectionClass
*
* @return string the serialization format marker, either self::SERIALIZATION_FORMAT_USE_UNSERIALIZER
* or self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER
*/
private function getSerializationFormat(ReflectionClass $reflectionClass)
{
if ($this->isPhpVersionWithBrokenSerializationFormat()
&& $reflectionClass->implementsInterface('Serializable')
) {
return self::SERIALIZATION_FORMAT_USE_UNSERIALIZER;
}

return self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER;
}

/**
* Checks whether the current PHP runtime uses an incompatible serialization format
*
* @return bool
*/
private function isPhpVersionWithBrokenSerializationFormat()
{
return PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513;
}

/**
* Checks if a class is cloneable
*
* @param ReflectionClass $reflection
*
* @return bool
* Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects.
*/
private function isSafeToClone(ReflectionClass $reflection)
private function isSafeToClone(ReflectionClass $reflection) : bool
{
if (method_exists($reflection, 'isCloneable') && ! $reflection->isCloneable()) {
return false;
}

// not cloneable if it implements `__clone`, as we want to avoid calling it
return ! $reflection->hasMethod('__clone');
return $reflection->isCloneable() && ! $reflection->hasMethod('__clone');
}
}
Loading