Skip to content

Commit

Permalink
Fix phpstan level 9 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Dec 1, 2022
1 parent aa9e1de commit c39c7f3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 8
level: 9
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
paths:
Expand Down
2 changes: 2 additions & 0 deletions spec/Prophecy/Argument/Token/ApproximateValueTokenSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function it_uses_a_default_precision_of_zero()
function it_does_not_score_if_rounded_argument_is_not_numeric()
{
$this->scoreArgument('hello')->shouldReturn(false);
$this->scoreArgument(new \stdClass())->shouldReturn(false);
$this->scoreArgument(false)->shouldReturn(false);
}

function it_has_simple_string_representation()
Expand Down
4 changes: 4 additions & 0 deletions src/Prophecy/Argument/Token/ApproximateValueToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function __construct($value, $precision = 0)
*/
public function scoreArgument($argument)
{
if (!\is_float($argument) && !\is_int($argument) && !\is_numeric($argument)) {
return false;
}

return round((float)$argument, $this->precision) === round($this->value, $this->precision) ? 10 : false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Prophecy/Argument/Token/ArrayCountToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function __toString()
*
* @param mixed $argument
* @return bool
*
* @phpstan-assert-if-true array<mixed>|\Countable $argument
*/
private function isCountable($argument)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Prophecy/Argument/Token/ArrayEntryToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ private function convertArrayAccessToEntry(\ArrayAccess $object)

$key = $this->key->getValue();

if (!\is_int($key) && !\is_string($key)) {
throw new InvalidArgumentException(sprintf(
'You can only use integer or string keys to match key of ArrayAccess object'.PHP_EOL.
'But you used `%s`.',
$this->key
));
}

return $object->offsetExists($key) ? array($key => $object[$key]) : array();
}
}
1 change: 1 addition & 0 deletions src/Prophecy/Comparator/ProphecyComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function accepts($expected, $actual): bool
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array()): void
{
\assert($actual instanceof ProphecyInterface);
parent::assertEquals($expected, $actual->reveal(), $delta, $canonicalize, $ignoreCase, $processed);
}
}
5 changes: 4 additions & 1 deletion src/Prophecy/Prophecy/ObjectProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function getMethodProphecies($methodName = null)
public function makeProphecyMethodCall($methodName, array $arguments)
{
$arguments = $this->revealer->reveal($arguments);
\assert(\is_array($arguments));
$return = $this->callCenter->makeCall($this, $methodName, $arguments);

return $this->revealer->reveal($return);
Expand Down Expand Up @@ -240,7 +241,9 @@ public function checkProphecyMethodsPredictions()
*/
public function __call($methodName, array $arguments)
{
$arguments = new ArgumentsWildcard($this->revealer->reveal($arguments));
$arguments = $this->revealer->reveal($arguments);
\assert(\is_array($arguments));
$arguments = new ArgumentsWildcard($arguments);

foreach ($this->getMethodProphecies($methodName) as $prophecy) {
$argumentsWildcard = $prophecy->getArgumentsWildcard();
Expand Down
12 changes: 7 additions & 5 deletions src/Prophecy/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct($verbose = true)
*/
public function stringify($value, $exportObject = true)
{
if (is_array($value)) {
if (\is_array($value)) {
if (range(0, count($value) - 1) === array_keys($value)) {
return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']';
}
Expand All @@ -52,16 +52,16 @@ public function stringify($value, $exportObject = true)
' => '.call_user_func($stringify, $item);
}, $value, array_keys($value))).']';
}
if (is_resource($value)) {
if (\is_resource($value)) {
return get_resource_type($value).':'.$value;
}
if (is_object($value)) {
if (\is_object($value)) {
return $exportObject ? ExportUtil::export($value) : sprintf('%s#%s', get_class($value), spl_object_id($value));
}
if (true === $value || false === $value) {
if (\is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_string($value)) {
if (\is_string($value)) {
$str = sprintf('"%s"', str_replace("\n", '\\n', $value));

if (!$this->verbose && 50 <= strlen($str)) {
Expand All @@ -74,6 +74,8 @@ public function stringify($value, $exportObject = true)
return 'null';
}

\assert(\is_int($value) || \is_float($value));

return (string) $value;
}

Expand Down

0 comments on commit c39c7f3

Please sign in to comment.