Skip to content

Commit

Permalink
Merge cf7508d into 7a5c815
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Oct 1, 2020
2 parents 7a5c815 + cf7508d commit d6e1556
Show file tree
Hide file tree
Showing 35 changed files with 792 additions and 2,082 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ $ vendor/bin/php-cs-fixer --dry-run --diff --verbose

### Compatibility

Your code **must** work on PHP from version 5.6 to the latest stable.
Your code **must** work on PHP from version 7.1 to the latest stable.

When you need to use a feature that is only available on PHP version greater than 5.6, you **must** either:
When you need to use a feature that is only available on PHP version greater than 7.1, you **must** either:

* Use version sniffing (`version_compare`),
* Check if class exists,
Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ clone_folder: C:\projects\atoum

environment:
matrix:
- PHP_DOWNLOAD_FILE: https://windows.php.net/downloads/releases/archives/php-5.6.40-nts-Win32-VC11-x86.zip
- PHP_DOWNLOAD_FILE: https://windows.php.net/downloads/releases/archives/php-7.1.33-nts-Win32-VC14-x86.zip
- PHP_DOWNLOAD_FILE: https://windows.php.net/downloads/releases/latest/php-7.2-nts-Win32-VC15-x86-latest.zip
- PHP_DOWNLOAD_FILE: https://windows.php.net/downloads/releases/latest/php-7.3-nts-Win32-VC15-x86-latest.zip
Expand Down
19 changes: 5 additions & 14 deletions classes/asserters/exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,9 @@ public function setWith($value, $checkType = true)
if ($exception instanceof \closure) {
$exception = null;

if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
try {
$value($this->getTest());
} catch (\throwable $exception) {
}
} else {
try {
$value();
} catch (\exception $exception) {
}
try {
$value($this->getTest());
} catch (\throwable $exception) {
}
}

Expand Down Expand Up @@ -143,13 +136,11 @@ protected function check($value, $method)

private static function isThrowable($value)
{
return $value instanceof \exception || (version_compare(PHP_VERSION, '7.0.0') >= 0 && ($value instanceof \throwable));
return $value instanceof \throwable;
}

private static function isThrowableClass($value)
{
return strtolower(ltrim($value, '\\')) === 'exception' ||
(version_compare(PHP_VERSION, '7.0.0') >= 0 && strtolower(ltrim($value, '\\')) === 'throwable') ||
is_subclass_of($value, version_compare(PHP_VERSION, '7.0.0') >= 0 ? 'throwable' : 'exception');
return strtolower(ltrim($value, '\\')) === 'throwable' || is_subclass_of($value, 'throwable');
}
}
4 changes: 2 additions & 2 deletions classes/includer.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function getFirstError()
return $firstError;
}

public function errorHandler($error, $message, $file, $line, $context)
public function errorHandler($error, $message, $file, $line, $context = [])
{
$errorReporting = $this->adapter->error_reporting();

if ($errorReporting !== 0 && ($errorReporting & $error)) {
if ($errorReporting & $error) {
foreach (array_reverse(debug_backtrace()) as $trace) {
if (isset($trace['file']) === true && $trace['file'] === $this->path) {
$file = $this->path;
Expand Down
2 changes: 1 addition & 1 deletion classes/iterators/recursives/directory/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use mageekguy\atoum\iterators\filters;

class factory implements \iteratorAggregate
class factory
{
protected $dotFilterFactory = null;
protected $iteratorFactory = null;
Expand Down
49 changes: 17 additions & 32 deletions classes/mock/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class generator
const defaultNamespace = 'mock';

protected $adapter = null;
protected $parameterAnalyzer = null;
protected $reflectionClassFactory = null;
protected $shuntedMethods = [];
protected $overloadedMethods = [];
Expand All @@ -27,6 +28,7 @@ public function __construct()
{
$this
->setAdapter()
->setParameterAnalyzer()
->setReflectionClassFactory()
;
}
Expand All @@ -48,6 +50,18 @@ public function getAdapter()
return $this->adapter;
}

public function setParameterAnalyzer(atoum\tools\parameter\analyzer $analyzer = null)
{
$this->parameterAnalyzer = $analyzer ?: new atoum\tools\parameter\analyzer();

return $this;
}

public function getParameterAnalyzer()
{
return $this->parameterAnalyzer;
}

public function setReflectionClassFactory(\closure $factory = null)
{
$this->reflectionClassFactory = $factory ?: function ($class) {
Expand Down Expand Up @@ -674,12 +688,12 @@ protected function getReturnType(\reflectionMethod $method)

protected function isNullable(\reflectionType $type)
{
return version_compare(PHP_VERSION, '7.0') >= 0 && $type->allowsNull() === true;
return $type->allowsNull() === true;
}

protected function hasReturnType(\reflectionMethod $method)
{
return version_compare(PHP_VERSION, '7.0') >= 0 && $method->hasReturnType() === true;
return $method->hasReturnType() === true;
}

protected function getReflectionType(\reflectionMethod $method)
Expand All @@ -697,13 +711,6 @@ protected function isVoid(\reflectionMethod $method)
return $this->hasReturnType($method) ? $this->getReflectionTypeName($method->getReturnType()) === 'void' : false;
}

protected static function isNullableParameter(\ReflectionParameter $parameter)
{
return version_compare(PHP_VERSION, '7.1') >= 0 &&
$parameter->allowsNull() &&
(!$parameter->isDefaultValueAvailable() || ($parameter->isDefaultValueAvailable() && null !== $parameter->getDefaultValue()));
}

protected static function isDefaultParameterNull(\ReflectionParameter $parameter)
{
return $parameter->allowsNull() &&
Expand Down Expand Up @@ -737,7 +744,7 @@ protected function getParametersSignature(\reflectionMethod $method, $forceMockC
$mustBeNull = $this->isOrphanized($method->getName());

foreach ($method->getParameters() as $parameter) {
$parameterCode = self::getParameterType($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . ($parameter->isVariadic() == false ? '' : '... ') . '$' . $parameter->getName();
$parameterCode = $this->parameterAnalyzer->getTypeHintString($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . ($parameter->isVariadic() == false ? '' : '... ') . '$' . $parameter->getName();

switch (true) {
case $parameter->isDefaultValueAvailable():
Expand Down Expand Up @@ -773,28 +780,6 @@ protected static function getClassName($class)
return ($lastAntiSlash === false ? $class : substr($class, $lastAntiSlash + 1));
}

protected static function getParameterType(\reflectionParameter $parameter)
{
$prefix = self::isNullableParameter($parameter) ? '?' : '';
switch (true) {
case $parameter->isArray():
return $prefix . 'array ';

case method_exists($parameter, 'isCallable') && $parameter->isCallable():
return $prefix . 'callable ';

case ($class = $parameter->getClass()):
return $prefix . '\\' . $class->getName() . ' ';

case method_exists($parameter, 'hasType') && $parameter->hasType():
$type = $parameter->getType();
return $prefix . ($type instanceof \reflectionNamedType ? $type->getName() : (string) $type) . ' ';

default:
return '';
}
}

protected static function hasVariadic(\reflectionMethod $method)
{
$parameters = $method->getParameters();
Expand Down
12 changes: 12 additions & 0 deletions classes/php/mocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class mocker
protected $reflectedFunctionFactory = null;

protected static $adapter = null;
protected static $parameterAnalyzer = null;

public function __construct($defaultNamespace = '')
{
Expand Down Expand Up @@ -51,6 +52,17 @@ public static function getAdapter()
{
return static::$adapter;
}

public static function setParameterAnalyzer(atoum\tools\parameter\analyzer $analyzer = null)
{
static::$parameterAnalyzer = $analyzer ?: new atoum\tools\parameter\analyzer();
}

public static function getParameterAnalyzer()
{
static::$parameterAnalyzer;
}
}

mocker::setAdapter();
mocker::setParameterAnalyzer();
19 changes: 1 addition & 18 deletions classes/php/mocker/funktion.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected static function getParametersSignature(\reflectionFunction $function)
$parameters = [];

foreach (self::filterParameters($function) as $parameter) {
$parameterCode = self::getParameterType($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . '$' . $parameter->getName();
$parameterCode = static::$parameterAnalyzer->getTypeHintString($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . '$' . $parameter->getName();

switch (true) {
case $parameter->isDefaultValueAvailable():
Expand All @@ -159,23 +159,6 @@ protected static function getParameters(\reflectionFunction $function)
return 'array(' . implode(',', $parameters) . ')';
}

protected static function getParameterType(\reflectionParameter $parameter)
{
switch (true) {
case $parameter->isArray():
return 'array ';

case $parameter->isCallable():
return 'callable ';

case $class = $parameter->getClass():
return '\\' . $class->getName() . ' ';

default:
return '';
}
}

protected static function defineMockedFunction($namespace, $class, $function, \reflectionFunction $reflectedFunction = null)
{
eval(sprintf(
Expand Down
4 changes: 2 additions & 2 deletions classes/scripts/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function useConfigurationCallable(\closure $callback)
$runner = $this->runner;
$errors = [];

$this->adapter->set_error_handler(function ($error, $message, $file, $line, $context) use (&$errors) {
$this->adapter->set_error_handler(function ($error, $message, $file, $line, $context = []) use (&$errors) {
foreach (array_reverse(debug_backtrace()) as $trace) {
if (isset($trace['file']) === true && $trace['file'] === __DIR__) {
$file = __DIR__;
Expand Down Expand Up @@ -691,7 +691,7 @@ function () use (& $autorunner, $calledClass) {
set_error_handler(function ($error, $message, $file, $line) use ($autorunner) {
$errorReporting = error_reporting();

if ($errorReporting !== 0 && $errorReporting & $error) {
if ($errorReporting & $error) {
$autorunner->writeError($message . ' in ' . $file . ' at line ' . $line, $error);

exit(3);
Expand Down
11 changes: 8 additions & 3 deletions classes/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1515,11 +1515,16 @@ public function setDataProvider($testMethodName, $dataProvider = null)
foreach ($reflectedMethod->getParameters() as $parameter) {
$parameterProvider = new test\data\providers\mock($this->mockGenerator);

if (($parameterClass = $parameter->getClass()) === null) {
// TODO Handle Union types and "self" keyword ?
$parameterClassName = $parameter->hasType() && !$parameter->getType()->isBuiltin()
? $parameter->getType()->getName()
: null;

if ($parameterClassName === null) {
throw new exceptions\logic\invalidArgument('Could not generate a data provider for ' . $this->class . '::' . $testMethodName . '() because it has at least one argument which is not type-hinted with a class or interface name');
}

$parametersProvider->addProvider($parameterProvider->setClass($parameterClass->getName()));
$parametersProvider->addProvider($parameterProvider->setClass($parameterClassName));
}

$dataProvider = new test\data\set($parametersProvider);
Expand All @@ -1544,7 +1549,7 @@ public function errorHandler($errno, $errstr, $errfile, $errline)
$doNotCallDefaultErrorHandler = true;
$errorReporting = $this->adapter->error_reporting();

if ($errorReporting !== 0 && $errorReporting & $errno) {
if ($errorReporting & $errno) {
list($file, $line) = $this->getBacktrace();

$this->score->addError($file ?: ($errfile ?: $this->path), $this->class, $this->currentMethod, $line ?: $errline, $errno, trim($errstr), $errfile, $errline);
Expand Down
40 changes: 40 additions & 0 deletions classes/tools/parameter/analyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace mageekguy\atoum\tools\parameter;

class analyzer
{
public function getTypeHintString(\reflectionParameter $parameter)
{
if (!$parameter->hasType()) {
return '';
}

$parameterType = $parameter->getType();
$parameterTypes = $parameterType instanceof \ReflectionUnionType
? $parameterType->getTypes()
: [$parameterType];

$names = [];
$hasMixed = false;
foreach ($parameterTypes as $type) {
$name = $type instanceof \reflectionNamedType ? $type->getName() : (string) $type;
if ($name === 'self') {
$name = $parameter->getDeclaringClass()->getName();
}
$names[] = ($type instanceof \reflectionType && !$type->isBuiltin() ? '\\' : '') . $name;

$hasMixed = $hasMixed || $name === 'mixed';
}

$prefix = !empty($names) && $this->isNullableParameter($parameter) && !$hasMixed ? '?' : '';

return $prefix . implode('|', $names) . ' ';
}

protected function isNullableParameter(\ReflectionParameter $parameter)
{
return $parameter->allowsNull() &&
(!$parameter->isDefaultValueAvailable() || ($parameter->isDefaultValueAvailable() && null !== $parameter->getDefaultValue()));
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"require":
{
"php": "^5.6.0 || ^7.0.0 <7.5.0",
"php": "^5.6 || ^7.0 || ^8.0",
"ext-hash": "*",
"ext-json": "*",
"ext-tokenizer": "*",
Expand Down
2 changes: 1 addition & 1 deletion scripts/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($builder) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$builder->writeError($message);

exit($error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($compiler) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$compiler->writeError($message);

exit($error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($coverage) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$coverage->writeError($message);

exit($error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/git/pusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($pusher) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$pusher->writeError($message);

exit($error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/phar/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($generator) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$generator->writeError($message);

exit($error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/tagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

set_error_handler(
function ($error, $message, $file, $line) use ($tagger) {
if (error_reporting() !== 0) {
if (error_reporting() & $error) {
$tagger->writeError($message);

exit($error);
Expand Down

0 comments on commit d6e1556

Please sign in to comment.