Skip to content

Commit

Permalink
fix return type of parent calls for SplHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Sep 15, 2023
1 parent 234f77d commit 1da5ae9
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 12 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@
"patches/Stream.patch"
],
"jetbrains/phpstorm-stubs": [
"patches/Memcache.patch",
"patches/PDO.patch",
"patches/SessionHandler.patch"
"patches/SessionHandler.patch",
"patches/SplHeapExtract.patch"
],
"rector/rector": [
"patches/NameNodeMapper.patch"
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions patches/Memcache.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- memcache/memcache.php 2023-09-04 17:18:27.000000000 +0200
+++ memcache/memcache.php 2023-09-08 14:29:48.807174822 +0200
@@ -217,7 +217,7 @@
* The lowest byte of the int is reserved for pecl/memcache internal usage (e.g. to indicate
* compression and serialization status).
* </p>
- * @return string|array|false <p>
+ * @return mixed <p>
* Returns the string associated with the <b>key</b> or
* an array of found key-value pairs when <b>key</b> is an {@link https://php.net/manual/en/language.types.array.php array}.
* Returns <b>FALSE</b> on failure, <b>key</b> is not found or
11 changes: 11 additions & 0 deletions patches/SplHeapExtract.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- SPL/SPL_c1.php 2023-09-04 17:18:27.000000000 +0200
+++ SPL/SPL_c1.php 2023-09-11 17:01:25.336451975 +0200
@@ -1372,7 +1372,7 @@
/**
* Extracts a node from top of the heap and sift up.
* @link https://php.net/manual/en/splheap.extract.php
- * @return mixed The value of the extracted node.
+ * @return TValue The value of the extracted node.
*/
#[TentativeType]
public function extract(): mixed {}
5 changes: 3 additions & 2 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,18 +839,19 @@ private function createNativeMethodVariant(
);
}

$returnType = null;
if ($stubPhpDocReturnType !== null) {
$returnType = $stubPhpDocReturnType;
$phpDocReturnType = $stubPhpDocReturnType;
} else {
$returnType = TypehintHelper::decideType($methodSignature->getReturnType(), $phpDocReturnType);
}

return new FunctionVariantWithPhpDocs(
TemplateTypeMap::createEmpty(),
null,
$parameters,
$methodSignature->isVariadic(),
$returnType ?? $methodSignature->getReturnType(),
$returnType,
$phpDocReturnType ?? new MixedType(),
$methodSignature->getNativeReturnType(),
);
Expand Down
4 changes: 4 additions & 0 deletions src/Type/TypehintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public static function decideType(
?Type $phpDocType = null,
): Type
{
if ($type instanceof BenevolentUnionType) {
return $type;
}

if ($phpDocType !== null && !$phpDocType instanceof ErrorType) {
if ($phpDocType instanceof NeverType && $phpDocType->isExplicit()) {
return $phpDocType;
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9293.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullsafe-vs-scalar.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8517.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9867.php');
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9867.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Bug9867;

use function PHPStan\Testing\assertType;

/** @extends \SplMinHeap<\DateTime> */
class MyMinHeap extends \SplMinHeap
{
public function test(): void
{
assertType('DateTime', parent::current());
assertType('DateTime', parent::extract());
assertType('DateTime', parent::top());
}

public function insert(mixed $value): bool
{
assertType('DateTime', $value);

return parent::insert($value);
}

protected function compare(mixed $value1, mixed $value2)
{
assertType('DateTime', $value1);
assertType('DateTime', $value2);

return parent::compare($value1, $value2);
}
}

/** @extends \SplMaxHeap<\DateTime> */
class MyMaxHeap extends \SplMaxHeap
{
public function test(): void
{
assertType('DateTime', parent::current());
assertType('DateTime', parent::extract());
assertType('DateTime', parent::top());
}

public function insert(mixed $value): bool
{
assertType('DateTime', $value);

return parent::insert($value);
}

protected function compare(mixed $value1, mixed $value2)
{
assertType('DateTime', $value1);
assertType('DateTime', $value2);

return parent::compare($value1, $value2);
}
}

/** @extends \SplHeap<\DateTime> */
abstract class MyHeap extends \SplHeap
{
public function test(): void
{
assertType('DateTime', parent::current());
assertType('DateTime', parent::extract());
assertType('DateTime', parent::top());
}

public function insert(mixed $value): bool
{
assertType('DateTime', $value);

return parent::insert($value);
}
}

0 comments on commit 1da5ae9

Please sign in to comment.