commitall

This commit is contained in:
Sampanna Rimal
2024-07-10 18:28:19 +05:45
parent 140abda4e6
commit 9cd05ef3cb
15723 changed files with 4818733 additions and 0 deletions

View File

@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument;
/**
* Arguments wildcarding.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ArgumentsWildcard
{
/**
* @var Token\TokenInterface[]
*/
private $tokens = array();
private $string;
/**
* Initializes wildcard.
*
* @param array $arguments Array of argument tokens or values
*/
public function __construct(array $arguments)
{
foreach ($arguments as $argument) {
if (!$argument instanceof Token\TokenInterface) {
$argument = new Token\ExactValueToken($argument);
}
$this->tokens[] = $argument;
}
}
/**
* Calculates wildcard match score for provided arguments.
*
* @param array $arguments
*
* @return false|int False OR integer score (higher - better)
*/
public function scoreArguments(array $arguments)
{
if (0 == count($arguments) && 0 == count($this->tokens)) {
return 1;
}
$arguments = array_values($arguments);
$totalScore = 0;
foreach ($this->tokens as $i => $token) {
$argument = isset($arguments[$i]) ? $arguments[$i] : null;
if (1 >= $score = $token->scoreArgument($argument)) {
return false;
}
$totalScore += $score;
if (true === $token->isLast()) {
return $totalScore;
}
}
if (count($arguments) > count($this->tokens)) {
return false;
}
return $totalScore;
}
/**
* Returns string representation for wildcard.
*
* @return string
*/
public function __toString()
{
if (null === $this->string) {
$this->string = implode(', ', array_map(function ($token) {
return (string) $token;
}, $this->tokens));
}
return $this->string;
}
/**
* @return array
*/
public function getTokens()
{
return $this->tokens;
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Any single value token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class AnyValueToken implements TokenInterface
{
/**
* Always scores 3 for any argument.
*
* @param $argument
*
* @return int
*/
public function scoreArgument($argument)
{
return 3;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return '*';
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Any values token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class AnyValuesToken implements TokenInterface
{
/**
* Always scores 2 for any argument.
*
* @param $argument
*
* @return int
*/
public function scoreArgument($argument)
{
return 2;
}
/**
* Returns true to stop wildcard from processing other tokens.
*
* @return bool
*/
public function isLast()
{
return true;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return '* [, ...]';
}
}

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Approximate value token
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class ApproximateValueToken implements TokenInterface
{
private $value;
private $precision;
public function __construct($value, $precision = 0)
{
$this->value = $value;
$this->precision = $precision;
}
/**
* {@inheritdoc}
*/
public function scoreArgument($argument)
{
return round($argument, $this->precision) === round($this->value, $this->precision) ? 10 : false;
}
/**
* {@inheritdoc}
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('≅%s', round($this->value, $this->precision));
}
}

View File

@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Array elements count token.
*
* @author Boris Mikhaylov <kaguxmail@gmail.com>
*/
class ArrayCountToken implements TokenInterface
{
private $count;
/**
* @param integer $value
*/
public function __construct($value)
{
$this->count = $value;
}
/**
* Scores 6 when argument has preset number of elements.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
return $this->isCountable($argument) && $this->hasProperCount($argument) ? 6 : false;
}
/**
* Returns false.
*
* @return boolean
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('count(%s)', $this->count);
}
/**
* Returns true if object is either array or instance of \Countable
*
* @param $argument
* @return bool
*/
private function isCountable($argument)
{
return (is_array($argument) || $argument instanceof \Countable);
}
/**
* Returns true if $argument has expected number of elements
*
* @param array|\Countable $argument
*
* @return bool
*/
private function hasProperCount($argument)
{
return $this->count === count($argument);
}
}

View File

@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use Prophecy\Exception\InvalidArgumentException;
/**
* Array entry token.
*
* @author Boris Mikhaylov <kaguxmail@gmail.com>
*/
class ArrayEntryToken implements TokenInterface
{
/** @var \Prophecy\Argument\Token\TokenInterface */
private $key;
/** @var \Prophecy\Argument\Token\TokenInterface */
private $value;
/**
* @param mixed $key exact value or token
* @param mixed $value exact value or token
*/
public function __construct($key, $value)
{
$this->key = $this->wrapIntoExactValueToken($key);
$this->value = $this->wrapIntoExactValueToken($value);
}
/**
* Scores half of combined scores from key and value tokens for same entry. Capped at 8.
* If argument implements \ArrayAccess without \Traversable, then key token is restricted to ExactValueToken.
*
* @param array|\ArrayAccess|\Traversable $argument
*
* @throws \Prophecy\Exception\InvalidArgumentException
* @return bool|int
*/
public function scoreArgument($argument)
{
if ($argument instanceof \Traversable) {
$argument = iterator_to_array($argument);
}
if ($argument instanceof \ArrayAccess) {
$argument = $this->convertArrayAccessToEntry($argument);
}
if (!is_array($argument) || empty($argument)) {
return false;
}
$keyScores = array_map(array($this->key,'scoreArgument'), array_keys($argument));
$valueScores = array_map(array($this->value,'scoreArgument'), $argument);
$scoreEntry = function ($value, $key) {
return $value && $key ? min(8, ($key + $value) / 2) : false;
};
return max(array_map($scoreEntry, $valueScores, $keyScores));
}
/**
* Returns false.
*
* @return boolean
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('[..., %s => %s, ...]', $this->key, $this->value);
}
/**
* Returns key
*
* @return TokenInterface
*/
public function getKey()
{
return $this->key;
}
/**
* Returns value
*
* @return TokenInterface
*/
public function getValue()
{
return $this->value;
}
/**
* Wraps non token $value into ExactValueToken
*
* @param $value
* @return TokenInterface
*/
private function wrapIntoExactValueToken($value)
{
return $value instanceof TokenInterface ? $value : new ExactValueToken($value);
}
/**
* Converts instance of \ArrayAccess to key => value array entry
*
* @param \ArrayAccess $object
*
* @return array|null
* @throws \Prophecy\Exception\InvalidArgumentException
*/
private function convertArrayAccessToEntry(\ArrayAccess $object)
{
if (!$this->key instanceof ExactValueToken) {
throw new InvalidArgumentException(sprintf(
'You can only use exact value tokens to match key of ArrayAccess object'.PHP_EOL.
'But you used `%s`.',
$this->key
));
}
$key = $this->key->getValue();
return $object->offsetExists($key) ? array($key => $object[$key]) : array();
}
}

View File

@ -0,0 +1,82 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Array every entry token.
*
* @author Adrien Brault <adrien.brault@gmail.com>
*/
class ArrayEveryEntryToken implements TokenInterface
{
/**
* @var TokenInterface
*/
private $value;
/**
* @param mixed $value exact value or token
*/
public function __construct($value)
{
if (!$value instanceof TokenInterface) {
$value = new ExactValueToken($value);
}
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function scoreArgument($argument)
{
if (!$argument instanceof \Traversable && !is_array($argument)) {
return false;
}
$scores = array();
foreach ($argument as $key => $argumentEntry) {
$scores[] = $this->value->scoreArgument($argumentEntry);
}
if (empty($scores) || in_array(false, $scores, true)) {
return false;
}
return array_sum($scores) / count($scores);
}
/**
* {@inheritdoc}
*/
public function isLast()
{
return false;
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return sprintf('[%s, ..., %s]', $this->value, $this->value);
}
/**
* @return TokenInterface
*/
public function getValue()
{
return $this->value;
}
}

View File

@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use Prophecy\Exception\InvalidArgumentException;
/**
* Callback-verified token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class CallbackToken implements TokenInterface
{
private $callback;
/**
* Initializes token.
*
* @param callable $callback
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function __construct($callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException(sprintf(
'Callable expected as an argument to CallbackToken, but got %s.',
gettype($callback)
));
}
$this->callback = $callback;
}
/**
* Scores 7 if callback returns true, false otherwise.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
return call_user_func($this->callback, $argument) ? 7 : false;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return 'callback()';
}
}

View File

@ -0,0 +1,118 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use Prophecy\Util\StringUtil;
/**
* Exact value token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ExactValueToken implements TokenInterface
{
private $value;
private $string;
private $util;
private $comparatorFactory;
/**
* Initializes token.
*
* @param mixed $value
* @param StringUtil $util
* @param ComparatorFactory $comparatorFactory
*/
public function __construct($value, StringUtil $util = null, ComparatorFactory $comparatorFactory = null)
{
$this->value = $value;
$this->util = $util ?: new StringUtil();
$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
}
/**
* Scores 10 if argument matches preset value.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
if (is_object($argument) && is_object($this->value)) {
$comparator = $this->comparatorFactory->getComparatorFor(
$argument, $this->value
);
try {
$comparator->assertEquals($argument, $this->value);
return 10;
} catch (ComparisonFailure $failure) {
return false;
}
}
// If either one is an object it should be castable to a string
if (is_object($argument) xor is_object($this->value)) {
if (is_object($argument) && !method_exists($argument, '__toString')) {
return false;
}
if (is_object($this->value) && !method_exists($this->value, '__toString')) {
return false;
}
} elseif (is_numeric($argument) && is_numeric($this->value)) {
// noop
} elseif (gettype($argument) !== gettype($this->value)) {
return false;
}
return $argument == $this->value ? 10 : false;
}
/**
* Returns preset value against which token checks arguments.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
if (null === $this->string) {
$this->string = sprintf('exact(%s)', $this->util->stringify($this->value));
}
return $this->string;
}
}

View File

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use Prophecy\Util\StringUtil;
/**
* Identical value token.
*
* @author Florian Voutzinos <florian@voutzinos.com>
*/
class IdenticalValueToken implements TokenInterface
{
private $value;
private $string;
private $util;
/**
* Initializes token.
*
* @param mixed $value
* @param StringUtil $util
*/
public function __construct($value, StringUtil $util = null)
{
$this->value = $value;
$this->util = $util ?: new StringUtil();
}
/**
* Scores 11 if argument matches preset value.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
return $argument === $this->value ? 11 : false;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
if (null === $this->string) {
$this->string = sprintf('identical(%s)', $this->util->stringify($this->value));
}
return $this->string;
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Logical AND token.
*
* @author Boris Mikhaylov <kaguxmail@gmail.com>
*/
class LogicalAndToken implements TokenInterface
{
private $tokens = array();
/**
* @param array $arguments exact values or tokens
*/
public function __construct(array $arguments)
{
foreach ($arguments as $argument) {
if (!$argument instanceof TokenInterface) {
$argument = new ExactValueToken($argument);
}
$this->tokens[] = $argument;
}
}
/**
* Scores maximum score from scores returned by tokens for this argument if all of them score.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
if (0 === count($this->tokens)) {
return false;
}
$maxScore = 0;
foreach ($this->tokens as $token) {
$score = $token->scoreArgument($argument);
if (false === $score) {
return false;
}
$maxScore = max($score, $maxScore);
}
return $maxScore;
}
/**
* Returns false.
*
* @return boolean
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('bool(%s)', implode(' AND ', $this->tokens));
}
}

View File

@ -0,0 +1,73 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Logical NOT token.
*
* @author Boris Mikhaylov <kaguxmail@gmail.com>
*/
class LogicalNotToken implements TokenInterface
{
/** @var \Prophecy\Argument\Token\TokenInterface */
private $token;
/**
* @param mixed $value exact value or token
*/
public function __construct($value)
{
$this->token = $value instanceof TokenInterface? $value : new ExactValueToken($value);
}
/**
* Scores 4 when preset token does not match the argument.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
return false === $this->token->scoreArgument($argument) ? 4 : false;
}
/**
* Returns true if preset token is last.
*
* @return bool|int
*/
public function isLast()
{
return $this->token->isLast();
}
/**
* Returns originating token.
*
* @return TokenInterface
*/
public function getOriginatingToken()
{
return $this->token;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('not(%s)', $this->token);
}
}

View File

@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use Prophecy\Util\StringUtil;
/**
* Object state-checker token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ObjectStateToken implements TokenInterface
{
private $name;
private $value;
private $util;
private $comparatorFactory;
/**
* Initializes token.
*
* @param string $methodName
* @param mixed $value Expected return value
* @param null|StringUtil $util
* @param ComparatorFactory $comparatorFactory
*/
public function __construct(
$methodName,
$value,
StringUtil $util = null,
ComparatorFactory $comparatorFactory = null
) {
$this->name = $methodName;
$this->value = $value;
$this->util = $util ?: new StringUtil;
$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
}
/**
* Scores 8 if argument is an object, which method returns expected value.
*
* @param mixed $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
if (is_object($argument) && method_exists($argument, $this->name)) {
$actual = call_user_func(array($argument, $this->name));
$comparator = $this->comparatorFactory->getComparatorFor(
$this->value, $actual
);
try {
$comparator->assertEquals($this->value, $actual);
return 8;
} catch (ComparisonFailure $failure) {
return false;
}
}
if (is_object($argument) && property_exists($argument, $this->name)) {
return $argument->{$this->name} === $this->value ? 8 : false;
}
return false;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('state(%s(), %s)',
$this->name,
$this->util->stringify($this->value)
);
}
}

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* String contains token.
*
* @author Peter Mitchell <pete@peterjmit.com>
*/
class StringContainsToken implements TokenInterface
{
private $value;
/**
* Initializes token.
*
* @param string $value
*/
public function __construct($value)
{
$this->value = $value;
}
public function scoreArgument($argument)
{
return is_string($argument) && strpos($argument, $this->value) !== false ? 6 : false;
}
/**
* Returns preset value against which token checks arguments.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('contains("%s")', $this->value);
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
/**
* Argument token interface.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
interface TokenInterface
{
/**
* Calculates token match score for provided argument.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument);
/**
* Returns true if this token prevents check of other tokens (is last one).
*
* @return bool|int
*/
public function isLast();
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString();
}

View File

@ -0,0 +1,76 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Argument\Token;
use Prophecy\Exception\InvalidArgumentException;
/**
* Value type token.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class TypeToken implements TokenInterface
{
private $type;
/**
* @param string $type
*/
public function __construct($type)
{
$checker = "is_{$type}";
if (!function_exists($checker) && !interface_exists($type) && !class_exists($type)) {
throw new InvalidArgumentException(sprintf(
'Type or class name expected as an argument to TypeToken, but got %s.', $type
));
}
$this->type = $type;
}
/**
* Scores 5 if argument has the same type this token was constructed with.
*
* @param $argument
*
* @return bool|int
*/
public function scoreArgument($argument)
{
$checker = "is_{$this->type}";
if (function_exists($checker)) {
return call_user_func($checker, $argument) ? 5 : false;
}
return $argument instanceof $this->type ? 5 : false;
}
/**
* Returns false.
*
* @return bool
*/
public function isLast()
{
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString()
{
return sprintf('type(%s)', $this->type);
}
}