Skip to content

Commit

Permalink
Implement proper global function/constant hoisting
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 7, 2025
1 parent d5ee11c commit 1a432ff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
8 changes: 8 additions & 0 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,14 @@ public function getStubbedConstantType(string $const_id): ?Union
return self::$stubbed_constants[$const_id] ?? null;
}

/**
* @return array<string, Union>

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

MismatchingDocblockReturnType

src/Psalm/Codebase.php:593:16: MismatchingDocblockReturnType: Docblock has incorrect return type 'array<string, Psalm\Type\Union>', should be 'void' (see https://psalm.dev/142)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

InvalidReturnType

src/Psalm/Codebase.php:593:16: InvalidReturnType: No return statements were found for method Psalm\Codebase::addGlobalConstantTypes but return type 'array<string, Psalm\Type\Union>' was expected (see https://psalm.dev/011)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

MismatchingDocblockReturnType

src/Psalm/Codebase.php:593:16: MismatchingDocblockReturnType: Docblock has incorrect return type 'array<string, Psalm\Type\Union>', should be 'void' (see https://psalm.dev/142)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

InvalidReturnType

src/Psalm/Codebase.php:593:16: InvalidReturnType: No return statements were found for method Psalm\Codebase::addGlobalConstantTypes but return type 'array<string, Psalm\Type\Union>' was expected (see https://psalm.dev/011)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

MismatchingDocblockReturnType

src/Psalm/Codebase.php:593:16: MismatchingDocblockReturnType: Docblock has incorrect return type 'array<string, Psalm\Type\Union>', should be 'void' (see https://psalm.dev/142)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

InvalidReturnType

src/Psalm/Codebase.php:593:16: InvalidReturnType: No return statements were found for method Psalm\Codebase::addGlobalConstantTypes but return type 'array<string, Psalm\Type\Union>' was expected (see https://psalm.dev/011)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

MismatchingDocblockReturnType

src/Psalm/Codebase.php:593:16: MismatchingDocblockReturnType: Docblock has incorrect return type 'array<string, Psalm\Type\Union>', should be 'void' (see https://psalm.dev/142)

Check failure on line 593 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

InvalidReturnType

src/Psalm/Codebase.php:593:16: InvalidReturnType: No return statements were found for method Psalm\Codebase::addGlobalConstantTypes but return type 'array<string, Psalm\Type\Union>' was expected (see https://psalm.dev/011)
*/
public function addGlobalConstantTypes(array $stubs): void

Check failure on line 595 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

UnusedParam

src/Psalm/Codebase.php:595:50: UnusedParam: Param #1 is never referenced in this method (see https://psalm.dev/135)

Check failure on line 595 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

UnusedParam

src/Psalm/Codebase.php:595:50: UnusedParam: Param #1 is never referenced in this method (see https://psalm.dev/135)

Check failure on line 595 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

UnusedParam

src/Psalm/Codebase.php:595:50: UnusedParam: Param #1 is never referenced in this method (see https://psalm.dev/135)

Check failure on line 595 in src/Psalm/Codebase.php

View workflow job for this annotation

GitHub Actions / build

UnusedParam

src/Psalm/Codebase.php:595:50: UnusedParam: Param #1 is never referenced in this method (see https://psalm.dev/135)
{
self::$stubbed_constants += $stubs;
}

/**
* @return array<string, Union>
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Internal/Codebase/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ public function addGlobalFunction(string $function_id, FunctionStorage $storage)
self::$stubbed_functions[strtolower($function_id)] = $storage;
}

/**
* @return array<string, FunctionStorage>
*/
public function addGlobalFunctions(array $stubs): void
{
self::$stubbed_functions += $stubs;
}

public function hasStubbedFunction(string $function_id): bool
{
return isset(self::$stubbed_functions[strtolower($function_id)]);
Expand Down
32 changes: 6 additions & 26 deletions src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
* classlike_storage:array<string, ClassLikeStorage>,
* file_storage:array<lowercase-string, FileStorage>,
* new_file_content_hashes: array<string, string>,
* taint_data: ?TaintFlowGraph
* taint_data: ?TaintFlowGraph,
* global_constants: array<string, Union>,
* global_functions: array<string, FunctionStorage>
* }
*/

Expand Down Expand Up @@ -352,6 +354,9 @@ private function scanFilePaths(int $pool_size): bool
$pool_data['new_file_content_hashes'],
);
}

$this->codebase->addGlobalConstantTypes($pool['global_constants']);
$this->codebase->functions->addGlobalFunctions($pool['global_functions']);
}
} else {
foreach ($files_to_scan as $file_path => $_) {
Expand All @@ -364,31 +369,6 @@ private function scanFilePaths(int $pool_size): bool
$this->codebase->statements_provider->parser_cache_provider->saveFileContentHashes();
}

foreach ($files_to_scan as $scanned_file) {
if ($this->codebase->all_functions_global || $this->config->hasStubFile($scanned_file)) {
$file_storage = $this->file_storage_provider->get($scanned_file);

foreach ($file_storage->functions as $function_storage) {
if ($function_storage->cased_name
&& !$this->codebase->functions->hasStubbedFunction($function_storage->cased_name)
) {
$this->codebase->functions->addGlobalFunction(
$function_storage->cased_name,
$function_storage,
);
}
}
}

if ($this->codebase->all_constants_global || $this->config->hasStubFile($scanned_file)) {
$file_storage = $this->file_storage_provider->get($scanned_file);

foreach ($file_storage->constants as $name => $type) {
$this->codebase->addGlobalConstantType($name, $type);
}
}
}

$this->file_reference_provider->addClassLikeFiles($this->classlike_files);

return true;
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Fork/ShutdownScannerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function run(Channel $channel, Cancellation $cancellation): mixed
? $statements_provider->parser_cache_provider->getNewFileContentHashes()
: [],
'taint_data' => $codebase->taint_flow_graph,
'global_constants' => $codebase->getAllStubbedConstants(),
'global_functions' => $codebase->functions->getAllStubbedFunctions(),
];
}
}

0 comments on commit 1a432ff

Please sign in to comment.