Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,20 @@ jobs:
- script: |
cd e2e/bug-14305
../../bin/phpstan
- script: |
# A userland class named `Resource` is a class, not the `resource` pseudo-type,
# on every phpVersion: https://phpstan.org/r/47fe50ab-e9de-4e20-8093-b24400e53d66
# The `resource` native type in the pre-PHP 8 PhpStorm stubs is the pseudo-type,
# which is what https://github.com/phpstan/phpstan/issues/15141 was about.
cd e2e/bug-15141
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --no-progress --error-format raw")
echo "$OUTPUT"
../bashunit -a contains 'test.php:13:Parameter #1 $r of function doFoo expects Resource, resource given.' "$OUTPUT"
../bashunit -a equals '1' "$(echo "$OUTPUT" | grep -Ec '^/.+:[0-9]+:')"
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --no-progress --error-format raw -c php74.neon")
echo "$OUTPUT"
../bashunit -a contains 'test.php:13:Parameter #1 $r of function doFoo expects Resource, resource given.' "$OUTPUT"
../bashunit -a equals '1' "$(echo "$OUTPUT" | grep -Ec '^/.+:[0-9]+:')"
- script: |
cd e2e/composer-and-phpstan-version-config
composer install --ignore-platform-reqs
Expand Down
6 changes: 6 additions & 0 deletions e2e/bug-15141/php74.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 10
phpVersion: 70400
paths:
- test.php
- resource-pseudo-type.php
4 changes: 4 additions & 0 deletions e2e/bug-15141/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 10
paths:
- test.php
9 changes: 9 additions & 0 deletions e2e/bug-15141/resource-pseudo-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$content = '';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo === FALSE) {
throw new \RuntimeException('Cannot create finfo instance.');
}

$type = (string) finfo_buffer($finfo, $content);
15 changes: 15 additions & 0 deletions e2e/bug-15141/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class Resource {}

function doFoo(Resource $r):void {}

$r = new Resource();
doFoo($r);

/** @param mixed $m */
function doBar($m):void {
if (is_resource($m)) {
doFoo($m);
}
}
8 changes: 8 additions & 0 deletions src/Type/ParserNodeTypeToPHPStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public static function resolve($type, ?ClassReflection $classReflection): Type
} elseif ($type instanceof Name) {
$typeClassName = (string) $type;
$lowercasedClassName = strtolower($typeClassName);
if ($typeClassName === 'resource') {
// PhpStorm stubs describe pre-PHP 8 signatures with a `resource` native type.
// Compared case-sensitively on purpose: PHP has no `resource` native type,
// it reads such a typehint as a class name, so a userland class named
// `Resource` has to keep resolving to an object type.
return new ResourceType();
}

if ($classReflection !== null && in_array($lowercasedClassName, ['self', 'static'], true)) {
if ($lowercasedClassName === 'static') {
return new StaticType($classReflection);
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15141-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint < 8.0

namespace Bug15141Php7;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

function doFoo(): void
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo === false) {
return;
}

assertType('resource', $finfo);
assertNativeType('resource', $finfo);
}

function doBar(): void
{
$connection = pg_connect('');
if ($connection === false) {
return;
}

$result = pg_exec($connection, 'SELECT 1');
assertType('resource|false', $result);
if (is_resource($result)) {
assertType('resource', $result);
}

$lob = pg_loopen($connection, 1, 'r');
if (is_resource($lob)) {
assertType('resource', $lob);
}
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3109,4 +3109,20 @@ public function testBug15168(): void
$this->analyse([__DIR__ . '/data/bug-15168.php'], []);
}

#[RequiresPhp('< 8.0.0')]
public function testBug15141(): void
{
$this->analyse([__DIR__ . '/data/bug-15141.php'], []);
}

public function testBug15141ResourceClass(): void
{
$this->analyse([__DIR__ . '/data/bug-15141-resource-class.php'], [
[
'Parameter #1 $r of function bug15141TakesResourceClass expects Resource, resource given.',
16,
],
]);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-15141-resource-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

// The functions are not called doFoo()/doBar() on purpose: the class has to stay
// in the global namespace, and global function names leak into other tests.

class Resource {}

function bug15141TakesResourceClass(Resource $r):void {}

$r = new Resource();
bug15141TakesResourceClass($r);

/** @param mixed $m */
function bug15141TakesMixed($m):void {
if (is_resource($m)) {
bug15141TakesResourceClass($m);
}
}
91 changes: 91 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-15141.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Bug15141;

function doFoo(): void
{
$content = '';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo === FALSE) {
throw new \RuntimeException('Cannot create finfo instance.');
}

$type = (string) finfo_buffer($finfo, $content);
$type2 = (string) finfo_file($finfo, 'foo.txt');
}

function doBar(): void
{
$ch = curl_init();
if ($ch === false) {
throw new \RuntimeException('');
}

curl_getinfo($ch);
}

function doBaz(): void
{
$ftp = ftp_connect('example.com');
if ($ftp === false) {
throw new \RuntimeException('');
}

ftp_alloc($ftp, 1);
ftp_quit($ftp);
}

function doLorem(): void
{
$connection = pg_connect('');
if ($connection === false) {
throw new \RuntimeException('');
}

pg_clientencoding($connection);
pg_errormessage($connection);

$result = pg_query($connection, 'SELECT 1');
if ($result === false) {
throw new \RuntimeException('');
}

pg_fieldname($result, 1);
pg_fieldnum($result, 'foo');
pg_fieldsize($result, 1);
pg_fieldtype($result, 1);
pg_getlastoid($result);
pg_numfields($result);
pg_numrows($result);
pg_freeresult($result);
}

/** @param resource $r */
function takesResource($r): void
{
}

function doIpsum(): void
{
$connection = pg_connect('');
if ($connection === false) {
throw new \RuntimeException('');
}

$result = pg_exec($connection, 'SELECT 1');
if ($result === false) {
throw new \RuntimeException('');
}

takesResource($result);
pg_fetch_row($result);
pg_num_rows($result);

$lob = pg_loopen($connection, 1, 'r');
if ($lob === false) {
throw new \RuntimeException('');
}

pg_lo_read($lob, 1);
pg_lo_close($lob);
}
Loading