Skip to content

Commit 326c6ec

Browse files
committed
Improve function signatures with functionMap only for built-in symbols
1 parent 79e5e97 commit 326c6ec

File tree

8 files changed

+47
-16
lines changed

8 files changed

+47
-16
lines changed

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ private function createMethod(
579579
return new EnumCasesMethodReflection($declaringClass, $arrayBuilder->getArray());
580580
}
581581

582-
if ($this->signatureMapProvider->hasMethodSignature($declaringClassName, $methodReflection->getName())) {
582+
if (($declaringClass->isBuiltin() || $declaringClass->isEnum()) && $this->signatureMapProvider->hasMethodSignature($declaringClassName, $methodReflection->getName())) {
583583
$variantsByType = ['positional' => []];
584584
$throwType = null;
585585
$asserts = Assertions::createEmpty();
@@ -880,12 +880,14 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
880880
$isInternal = $resolvedPhpDoc->isInternal();
881881
$isFinal = $resolvedPhpDoc->isFinal();
882882
$isPure = null;
883-
foreach (array_keys($actualDeclaringClass->getAncestors()) as $className) {
884-
if ($this->signatureMapProvider->hasMethodMetadata($className, $methodReflection->getName())) {
885-
$hasSideEffects = $this->signatureMapProvider->getMethodMetadata($className, $methodReflection->getName())['hasSideEffects'];
886-
$isPure = !$hasSideEffects;
883+
if ($actualDeclaringClass->isBuiltin() || $actualDeclaringClass->isEnum()) {
884+
foreach (array_keys($actualDeclaringClass->getAncestors()) as $className) {
885+
if ($this->signatureMapProvider->hasMethodMetadata($className, $methodReflection->getName())) {
886+
$hasSideEffects = $this->signatureMapProvider->getMethodMetadata($className, $methodReflection->getName())['hasSideEffects'];
887+
$isPure = !$hasSideEffects;
887888

888-
break;
889+
break;
890+
}
889891
}
890892
}
891893

src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPStan\Type\TypehintHelper;
2525
use function array_key_exists;
2626
use function array_map;
27+
use function str_contains;
2728
use function strtolower;
2829

2930
#[AutowiredService]
@@ -75,6 +76,9 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
7576
$isDeprecated = $reflectionFunction->isDeprecated();
7677
if ($reflectionFunction->getFileName() !== null) {
7778
$fileName = $reflectionFunction->getFileName();
79+
if (!$reflectionFunctionAdapter->isInternal() && !str_contains(strtolower($fileName), 'polyfill')) {
80+
return null;
81+
}
7882
$docComment = $reflectionFunction->getDocComment();
7983
if ($docComment !== null) {
8084
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, null, null, $reflectionFunction->getName(), $docComment);

tests/PHPStan/Reflection/ParametersAcceptorSelectorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class ParametersAcceptorSelectorTest extends PHPStanTestCase
4444
*/
4545
public static function dataSelectFromTypes(): Generator
4646
{
47-
require_once __DIR__ . '/data/function-definitions.php';
4847
$reflectionProvider = self::createReflectionProvider();
4948

5049
$arrayRandVariants = $reflectionProvider->getFunction(new Name('array_rand'), null)->getVariants();
@@ -185,7 +184,7 @@ public static function dataSelectFromTypes(): Generator
185184
new MixedType(),
186185
PassedByReference::createNo(),
187186
true,
188-
null,
187+
new NullType(),
189188
),
190189
],
191190
true,

tests/PHPStan/Reflection/data/function-definitions.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,4 +2324,11 @@ public function testBug13197(): void
23242324
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-13197.php'], []);
23252325
}
23262326

2327+
public function testBug13556(): void
2328+
{
2329+
$this->checkExplicitMixed = true;
2330+
$this->checkImplicitMixed = true;
2331+
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
2332+
}
2333+
23272334
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
function swf_actiongotoframe($string) {
4+
5+
}
6+
7+
swf_actiongotoframe('string');

tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,4 +907,13 @@ public function testRestrictedInternalClassNameUsage(): void
907907
]);
908908
}
909909

910+
public function testBug13556(): void
911+
{
912+
$this->checkThisOnly = false;
913+
$this->checkExplicitMixed = true;
914+
$this->checkImplicitMixed = true;
915+
916+
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
917+
}
918+
910919
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class event {
4+
public static function add($_event, $_option = []) {
5+
// $_option est optionnel avec valeur par défaut
6+
}
7+
8+
public static function callAdd() {
9+
self::add('a',[]); // Appel avec 2 paramètres (1 requis + 1 optionnel)
10+
}
11+
}

0 commit comments

Comments
 (0)