Skip to content

Commit

Permalink
Fix PHP 8.1 deprecations in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 30, 2021
1 parent 1a43385 commit 50a7141
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -2562,7 +2562,7 @@ private function calculateFromScalars(Expr $node, ConstantScalarType $leftType,
}

if ($node instanceof Node\Expr\BinaryOp\Mod || $node instanceof Node\Expr\AssignOp\Mod) {
return $this->getTypeFromValue($leftNumberValue % $rightNumberValue);
return $this->getTypeFromValue(((int) $leftNumberValue) % ((int) $rightNumberValue));
}

if ($node instanceof Expr\BinaryOp\ShiftLeft || $node instanceof Expr\AssignOp\ShiftLeft) {
Expand Down
Expand Up @@ -144,7 +144,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
null,
null
);
$reflection->populateValue(constant($constantName));
$reflection->populateValue(@constant($constantName));

return $reflection;
}
Expand Down
Expand Up @@ -78,7 +78,7 @@ public function enterNode(\PhpParser\Node $node): ?int
$constantName = $nameNode->value;

if (defined($constantName)) {
$constantValue = constant($constantName);
$constantValue = @constant($constantName);
$node->getArgs()[1]->value = BuilderHelpers::normalizeValue($constantValue);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Runtime/RuntimeReflectionProvider.php
Expand Up @@ -350,7 +350,7 @@ public function getConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): Glob

return $this->cachedConstants[$constantName] = new RuntimeConstantReflection(
$constantName,
ConstantTypeHelper::getTypeFromValue(constant($constantName)),
ConstantTypeHelper::getTypeFromValue(@constant($constantName)),
null
);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Type/ArrayType.php
Expand Up @@ -309,8 +309,12 @@ public static function castToArrayKeyType(Type $offsetType): Type
}

if ($offsetType instanceof ConstantScalarType) {
$keyValue = $offsetType->getValue();
if (is_float($keyValue)) {
$keyValue = (int) $keyValue;
}
/** @var int|string $offsetValue */
$offsetValue = key([$offsetType->getValue() => null]);
$offsetValue = key([$keyValue => null]);
return is_int($offsetValue) ? new ConstantIntegerType($offsetValue) : new ConstantStringType($offsetValue);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/array-accessable.php
Expand Up @@ -34,6 +34,7 @@ public function returnSelfWithIterableInt(): self

}

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{

Expand All @@ -44,11 +45,13 @@ public function offsetGet($offset): int

}

#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{

}

#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-4715.php
Expand Up @@ -19,7 +19,7 @@ class ArrayCollection implements Collection
/**
* {@inheritDoc}
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator([]);
}
Expand Down
Expand Up @@ -162,6 +162,9 @@ public function testFormatErrors(
string $expected
): void
{
if (PHP_VERSION_ID >= 80100) {
self::markTestSkipped('Skipped on PHP 8.1 because of different result');
}
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new GithubErrorFormatter(
$relativePathHelper,
Expand Down
Expand Up @@ -150,6 +150,9 @@ public function testFormatErrors(
string $expected
): void
{
if (PHP_VERSION_ID >= 80100) {
self::markTestSkipped('Skipped on PHP 8.1 because of different result');
}
$formatter = new TableErrorFormatter(new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/'), false, null);

$this->assertSame($exitCode, $formatter->formatErrors(
Expand Down
Expand Up @@ -90,7 +90,7 @@ class ObjectWithOffsetAccess implements \ArrayAccess
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return true;
}
Expand All @@ -99,6 +99,7 @@ public function offsetExists($offset)
* @param string $offset
* @return int
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return 0;
Expand All @@ -109,15 +110,15 @@ public function offsetGet($offset)
* @param int $value
* @return void
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
}

/**
* @param string $offset
* @return void
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Classes/data/bug-4030.php
Expand Up @@ -9,7 +9,7 @@ public function __construct(\Traversable $iterator)

}

public function accept()
public function accept(): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Functions/data/bug-2268.php
Expand Up @@ -7,7 +7,7 @@ abstract class Message implements \ArrayAccess
/**
* @param string $value
*/
abstract public function offsetSet($key, $value);
abstract public function offsetSet($key, $value): void;
}


Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Generics/data/cross-check-interfaces.php
Expand Up @@ -18,7 +18,7 @@ interface ItemListInterface extends \Traversable
*/
final class ItemList implements \IteratorAggregate, ItemListInterface
{
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator([]);
}
Expand All @@ -29,7 +29,7 @@ public function getIterator()
*/
final class ItemList2 implements \IteratorAggregate, ItemListInterface
{
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator([]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Expand Up @@ -211,7 +211,7 @@ public function testBug3997(): void
$this->analyse([__DIR__ . '/data/bug-3997.php'], [
[
'Return type (string) of method Bug3997\Ipsum::count() should be compatible with return type (int) of method Countable::count()',
59,
63,
],
]);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -374,23 +374,23 @@ public function testBug3997(): void
$this->analyse([__DIR__ . '/data/bug-3997.php'], [
[
'Method Bug3997\Foo::count() should return int but returns string.',
12,
13,
],
[
'Method Bug3997\Bar::count() should return int but returns string.',
22,
24,
],
[
'Method Bug3997\Baz::count() should return int but returns string.',
35,
38,
],
[
'Method Bug3997\Lorem::count() should return int but returns string.',
48,
52,
],
[
'Method Bug3997\Dolor::count() should return int<0, max> but returns -1.',
72,
78,
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/data/bug-3034.php
Expand Up @@ -15,7 +15,7 @@ class HelloWorld implements \IteratorAggregate
/**
* @return \ArrayIterator<int, array{name: string, value: string}>
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->list);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Methods/data/bug-3478.php
Expand Up @@ -4,6 +4,7 @@

class ExtendedDocument extends \DOMDocument
{
#[\ReturnTypeWillChange]
public function saveHTML(\DOMNode $node = null)
{
return parent::saveHTML($node);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-3997.php
Expand Up @@ -7,6 +7,7 @@
class Foo implements Countable
{

#[\ReturnTypeWillChange]
public function count()
{
return 'foo';
Expand All @@ -17,6 +18,7 @@ public function count()
class Bar implements Countable
{

#[\ReturnTypeWillChange]
public function count(): int
{
return 'foo';
Expand All @@ -30,6 +32,7 @@ class Baz implements Countable
/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count(): int
{
return 'foo';
Expand All @@ -43,6 +46,7 @@ class Lorem implements Countable
/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
return 'foo';
Expand All @@ -56,6 +60,7 @@ class Ipsum implements Countable
/**
* @return string
*/
#[\ReturnTypeWillChange]
public function count()
{
return 'foo';
Expand All @@ -67,6 +72,7 @@ class Dolor implements Countable
{

/** @return positive-int|0 */
#[\ReturnTypeWillChange]
public function count(): int
{
return -1;
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Methods/data/bug-4084.php
Expand Up @@ -8,10 +8,10 @@ class Handler implements \SessionUpdateTimestampHandlerInterface
* @param string $sessionId
* @param string $data
*/
public function updateTimestamp($sessionId, $data) { return true; }
public function updateTimestamp($sessionId, $data): bool { return true; }

/**
* @param string $sessionId The session id
*/
public function validateId($sessionId) { return true; }
public function validateId($sessionId): bool { return true; }
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Methods/data/bug-4854.php
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractDomainsAvailability implements DomainsAvailabilityInterfa
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->domains);
}
Expand All @@ -43,7 +43,7 @@ public function offsetSet($offset, $value): void
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->domains[$offset]);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Methods/data/bug-5436.php
Expand Up @@ -4,6 +4,7 @@

final class PDO extends \PDO
{
#[\ReturnTypeWillChange]
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
return parent::query($query, $fetchMode, ...$fetchModeArgs);
Expand Down
Expand Up @@ -135,9 +135,9 @@ function (TestArrayObject2 $arrayObject2): void {
class TestArrayObject3 extends \ArrayObject
{

public function append($someValue)
public function append($someValue): void
{
return parent::append($someValue);
parent::append($someValue);
}

}
Expand Down
Expand Up @@ -155,7 +155,7 @@ interface SomeInterface
class Collection implements \IteratorAggregate
{

public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator([]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Type/UnionTypeTest.php
Expand Up @@ -308,7 +308,7 @@ public function dataIsSuperTypeOf(): \Iterator

yield [
$unionTypeB,
new ObjectType('Foo'),
new ObjectType(\stdClass::class),
TrinaryLogic::createNo(),
];

Expand Down Expand Up @@ -503,7 +503,7 @@ public function dataIsSubTypeOf(): \Iterator

yield [
$unionTypeB,
new ObjectType('Foo'),
new ObjectType(\stdClass::class),
TrinaryLogic::createNo(),
];
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Type/data/cyclic-phpdocs.php
Expand Up @@ -5,5 +5,6 @@
interface Foo extends \IteratorAggregate
{
/** @return iterable<Foo> | Foo */
#[\ReturnTypeWillChange]
public function getIterator();
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Type/data/dependent-phpdocs.php
Expand Up @@ -8,5 +8,5 @@ interface Foo extends \IteratorAggregate
public function addPages($pages);

/** non-empty */
public function getIterator();
public function getIterator(): \Traversable;
}

0 comments on commit 50a7141

Please sign in to comment.