Skip to content

Commit 0e16ad6

Browse files
committed
Simplify FunctionMapReader
PHP 8.0 has null-safe operators, which make operating on a single nullable type cleaner than having separate "does X exist" and "give me X" functions
1 parent d76c738 commit 0e16ad6

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

generator/src/PhpStanFunctions/PhpStanFunctionMapReader.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ public function __construct()
2424
$this->customFunctionMap = require FileCreator::getSafeRootDir() . '/generator/config/CustomPhpStanFunctionMap.php';
2525
}
2626

27-
public function hasFunction(string $functionName): bool
28-
{
29-
return isset($this->functionMap[$functionName]);
30-
}
31-
32-
public function getFunction(string $functionName): PhpStanFunction
27+
public function getFunction(string $functionName): ?PhpStanFunction
3328
{
29+
if (!isset($this->functionMap[$functionName])) {
30+
return null;
31+
}
3432
$map = $this->functionMap[$functionName];
3533
$customMap = $this->customFunctionMap[$functionName] ?? null;
3634
if ($map && $customMap) {

generator/src/XmlDocParser/Method.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public function __construct(
3131
PhpStanFunctionMapReader $phpStanFunctionMapReader,
3232
private int $errorType
3333
) {
34-
$functionName = $this->getFunctionName();
35-
$this->phpstanSignature = $phpStanFunctionMapReader->hasFunction($functionName) ? $phpStanFunctionMapReader->getFunction($functionName) : null;
34+
$this->phpstanSignature = $phpStanFunctionMapReader->getFunction($this->getFunctionName());
3635
$this->returnType = PhpStanType::selectMostUsefulType(
3736
$this->phpstanSignature?->getReturnType(),
3837
new PhpStanType($this->functionObject->type),

generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88

99
class PhpStanFunctionMapReaderTest extends TestCase
1010
{
11-
public function testHas(): void
12-
{
13-
$mapReader = new PhpStanFunctionMapReader();
14-
$this->assertTrue($mapReader->hasFunction('strpos'));
15-
$this->assertFalse($mapReader->hasFunction('foobar'));
16-
}
17-
1811
public function testGet(): void
1912
{
2013
$mapReader = new PhpStanFunctionMapReader();
21-
$function = $mapReader->getFunction('apcu_fetch');
2214

15+
$this->assertNull($mapReader->getFunction('foobar'));
16+
17+
$function = $mapReader->getFunction('apcu_fetch');
18+
$this->assertNotNull($function);
2319

2420
// 'apcu_fetch' => ['mixed', 'key'=>'string|string[]', '&w_success='=>'bool'],
2521
$this->assertSame('mixed', $function->getReturnType()->getDocBlockType());
@@ -28,7 +24,7 @@ public function testGet(): void
2824
$this->assertSame('success', $parameters['success']->getName());
2925
$this->assertSame('bool|null', $parameters['success']->getType()->getDocBlockType());
3026
}
31-
27+
3228
//todo: find a way to test custom map
3329
/*public function testCustomMapThrowExceptionIfOutdated()
3430
{

0 commit comments

Comments
 (0)