Skip to content

Commit 4382bc4

Browse files
committed
PHPStan: Change phpstan extensions to read recipes as string rather than include them
This fixes issues with having to execute more complex code in recipes that is not relevant to what we need for the extension
1 parent 0c37a3b commit 4382bc4

2 files changed

+26
-16
lines changed

src/Lunr/Core/PHPStan/ConfigServiceLocatorAsContainerInterfaceMethodReturnTypeExtension.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4141
return !in_array($methodReflection->getName(), [ 'has' ]);
4242
}
4343

44-
/**
45-
* Catch locator get calls in the locate files
46-
*
47-
* @param string $name Name of the locator
48-
*
49-
* @return void
50-
*/
51-
public function get(string $name): void
52-
{
53-
//NO-OP
54-
}
55-
5644
}
5745

5846
?>

src/Lunr/Core/PHPStan/ConfigServiceLocatorMethodReturnTypeExtension.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,41 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7979
/** @var LocatorRecipe $recipe */
8080
$recipe = [];
8181
$path = 'locator/locate.' . $id . '.inc.php';
82-
if (stream_resolve_include_path($path) === FALSE)
82+
$file = stream_resolve_include_path($path);
83+
84+
if ($file === FALSE)
8385
{
8486
return NULL;
8587
}
8688

87-
include_once $path;
89+
$class = NULL;
90+
91+
$handle = fopen($file, 'r');
92+
if ($handle !== FALSE)
93+
{
94+
while (($line = fgets($handle)) !== FALSE)
95+
{
96+
if (str_contains($line, '$recipe[\'' . $id . '\'][\'name\']'))
97+
{
98+
preg_match("/=\s*'([^']+)'/", $line, $matches);
99+
if (isset($matches[1]))
100+
{
101+
$class = $matches[1];
102+
}
103+
104+
break;
105+
}
106+
}
107+
108+
fclose($handle);
109+
}
88110

89-
if (!isset($recipe[$id]['name']))
111+
if ($class === NULL)
90112
{
91113
return NULL;
92114
}
93115

94-
return new ObjectType($recipe[$id]['name']);
116+
return new ObjectType($class);
95117
}
96118

97119
}

0 commit comments

Comments
 (0)