Skip to content

Commit

Permalink
return types (#4311)
Browse files Browse the repository at this point in the history
* return types

* remove willReturn for void methods
  • Loading branch information
orklah committed Oct 12, 2020
1 parent fcfa746 commit 10f2966
Show file tree
Hide file tree
Showing 99 changed files with 166 additions and 551 deletions.
4 changes: 1 addition & 3 deletions examples/TemplateScanner.php
Expand Up @@ -15,15 +15,13 @@ class TemplateScanner extends Psalm\Internal\Scanner\FileScanner

/**
* @param bool $storage_from_cache
*
* @return void
*/
public function scan(
Codebase $codebase,
FileStorage $file_storage,
$storage_from_cache = false,
?Progress $progress = null
) {
): void {
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
'7.4',
Expand Down
4 changes: 1 addition & 3 deletions examples/plugins/ClassUnqualifier.php
Expand Up @@ -11,16 +11,14 @@ class ClassUnqualifier implements AfterClassLikeExistenceCheckInterface
{
/**
* @param FileManipulation[] $file_replacements
*
* @return void
*/
public static function afterClassLikeExistenceCheck(
string $fq_class_name,
CodeLocation $code_location,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
): void {
$candidate_type = $code_location->getSelectedText();
$aliases = $statements_source->getAliasedClassesFlipped();

Expand Down
8 changes: 2 additions & 6 deletions examples/plugins/FunctionCasingChecker.php
Expand Up @@ -25,8 +25,6 @@ class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, After
/**
* @param MethodCall|StaticCall $expr
* @param FileManipulation[] $file_replacements
*
* @return void
*/
public static function afterMethodCallAnalysis(
Expr $expr,
Expand All @@ -38,7 +36,7 @@ public static function afterMethodCallAnalysis(
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
) {
): void {
if (!$expr->name instanceof PhpParser\Node\Identifier) {
return;
}
Expand Down Expand Up @@ -74,8 +72,6 @@ public static function afterMethodCallAnalysis(
/**
* @param non-empty-string $function_id
* @param FileManipulation[] $file_replacements
*
* @return void
*/
public static function afterFunctionCallAnalysis(
FuncCall $expr,
Expand All @@ -85,7 +81,7 @@ public static function afterFunctionCallAnalysis(
Codebase $codebase,
Union $return_type_candidate,
array &$file_replacements
) {
): void {
if ($expr->name instanceof PhpParser\Node\Expr) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/plugins/composer-based/echo-checker/EchoChecker.php
Expand Up @@ -26,7 +26,7 @@ public static function afterStatementAnalysis(
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
): ?bool {
if ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
foreach ($stmt->exprs as $expr) {
$expr_type = $statements_source->getNodeTypeProvider()->getType($expr);
Expand Down Expand Up @@ -67,5 +67,7 @@ public static function afterStatementAnalysis(
}
}
}

return null;
}
}
Expand Up @@ -6,8 +6,7 @@

class PluginEntryPoint implements Plugin\PluginEntryPointInterface
{
/** @return void */
public function __invoke(Plugin\RegistrationInterface $registration, ?SimpleXMLElement $config = null)
public function __invoke(Plugin\RegistrationInterface $registration, ?SimpleXMLElement $config = null): void
{
require_once __DIR__ . '/EchoChecker.php';
$registration->registerHooksFromClass(EchoChecker::class);
Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/CodeLocation.php
Expand Up @@ -138,10 +138,8 @@ public function setCommentLine(int $line): void

/**
* @psalm-suppress MixedArrayAccess
*
* @return void
*/
private function calculateRealLocation()
private function calculateRealLocation(): void
{
if ($this->have_recalculated) {
return;
Expand Down
10 changes: 2 additions & 8 deletions src/Psalm/Codebase.php
Expand Up @@ -873,10 +873,7 @@ public function getCasedMethodId($method_id): string
return $this->methods->getCasedMethodId(Internal\MethodIdentifier::wrap($method_id));
}

/**
* @return void
*/
public function invalidateInformationForFile(string $file_path)
public function invalidateInformationForFile(string $file_path): void
{
$this->scanner->removeFile($file_path);

Expand Down Expand Up @@ -968,10 +965,7 @@ public function getSymbolInformation(string $file_path, string $symbol): ?string
}
}

/**
* @return ?CodeLocation
*/
public function getSymbolLocation(string $file_path, string $symbol)
public function getSymbolLocation(string $file_path, string $symbol): ?CodeLocation
{
if (\is_numeric($symbol[0])) {
$symbol = \preg_replace('/:.*/', '', $symbol);
Expand Down
13 changes: 2 additions & 11 deletions src/Psalm/Context.php
Expand Up @@ -377,9 +377,6 @@ public function __destruct()
$this->parent_context = null;
}

/**
* @return void
*/
public function __clone()
{
foreach ($this->clauses as &$clause) {
Expand Down Expand Up @@ -635,15 +632,12 @@ public function removeVarFromConflictingClauses(
}
}

/**
* @return void
*/
public function removeDescendents(
string $remove_var_id,
?Union $existing_type = null,
?Union $new_type = null,
?StatementsAnalyzer $statements_analyzer = null
) {
): void {
if (!$existing_type && isset($this->vars_in_scope[$remove_var_id])) {
$existing_type = $this->vars_in_scope[$remove_var_id];
}
Expand All @@ -668,10 +662,7 @@ public function removeDescendents(
}
}

/**
* @return void
*/
public function removeAllObjectVars()
public function removeAllObjectVars(): void
{
$vars_to_remove = [];

Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/FileBasedPluginAdapter.php
Expand Up @@ -30,10 +30,8 @@ public function __construct(string $path, Config $config, Codebase $codebase)

/**
* @psalm-suppress PossiblyUnusedParam
*
* @return void
*/
public function __invoke(Plugin\RegistrationInterface $registration, ?SimpleXMLElement $config = null)
public function __invoke(Plugin\RegistrationInterface $registration, ?SimpleXMLElement $config = null): void
{
$fq_class_name = $this->getPluginClassForPath($this->path);

Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php
Expand Up @@ -28,16 +28,14 @@ class AlgebraAnalyzer
* @param array<int, Clause> $formula_1
* @param array<int, Clause> $formula_2
* @param array<string, bool> $new_assigned_var_ids
*
* @return void
*/
public static function checkForParadox(
array $formula_1,
array $formula_2,
StatementsAnalyzer $statements_analyzer,
PhpParser\Node $stmt,
array $new_assigned_var_ids
) {
): void {
try {
$negated_formula2 = Algebra::negateFormula($formula_2);
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
Expand Down
10 changes: 2 additions & 8 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1135,17 +1135,14 @@ public static function addContextProperties(
}
}

/**
* @return void
*/
private function checkPropertyInitialization(
Codebase $codebase,
Config $config,
ClassLikeStorage $storage,
Context $class_context,
?Context $global_context = null,
?MethodAnalyzer $constructor_analyzer = null
) {
): void {
if (!$config->reportIssueInFile('PropertyNotSetInConstructor', $this->getFilePath())) {
return;
}
Expand Down Expand Up @@ -1607,14 +1604,11 @@ private function analyzeTraitUse(
return null;
}

/**
* @return void
*/
private function checkForMissingPropertyType(
StatementsSource $source,
PhpParser\Node\Stmt\Property $stmt,
Context $context
) {
): void {
$fq_class_name = $source->getFQCLN();
$property_name = $stmt->props[0]->name->name;

Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Expand Up @@ -753,13 +753,12 @@ public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $commen

/**
* @param array<int, string> $return_specials
* @return void
*/
private static function extractReturnType(
PhpParser\Comment\Doc $comment,
array $return_specials,
FunctionDocblockComment $info
) {
): void {
foreach ($return_specials as $offset => $return_block) {
$return_lines = explode("\n", $return_block);

Expand Down
23 changes: 5 additions & 18 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Expand Up @@ -323,10 +323,7 @@ public function populateCheckers(array $stmts): array
return $leftover_stmts;
}

/**
* @return void
*/
private function populateClassLikeAnalyzers(PhpParser\Node\Stmt\ClassLike $stmt)
private function populateClassLikeAnalyzers(PhpParser\Node\Stmt\ClassLike $stmt): void
{
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
if (!$stmt->name) {
Expand Down Expand Up @@ -372,14 +369,11 @@ public function addNamespacedInterfaceAnalyzer(string $fq_class_name, InterfaceA
$this->interface_analyzers_to_analyze[strtolower($fq_class_name)] = $interface_analyzer;
}

/**
* @return void
*/
public function getMethodMutations(
\Psalm\Internal\MethodIdentifier $method_id,
Context $this_context,
bool $from_project_analyzer = false
) {
): void {
$fq_class_name = $method_id->fq_class_name;
$method_name = $method_id->method_name;
$fq_class_name_lc = strtolower($fq_class_name);
Expand Down Expand Up @@ -515,10 +509,7 @@ public function getRootFilePath(): string
return $this->root_file_path ?: $this->file_path;
}

/**
* @return void
*/
public function setRootFilePath(string $file_path, string $file_name)
public function setRootFilePath(string $file_path, string $file_name): void
{
$this->root_file_name = $file_name;
$this->root_file_path = $file_path;
Expand Down Expand Up @@ -575,10 +566,8 @@ public function getSuppressedIssues(): array

/**
* @param array<int, string> $new_issues
*
* @return void
*/
public function addSuppressedIssues(array $new_issues)
public function addSuppressedIssues(array $new_issues): void
{
if (isset($new_issues[0])) {
$new_issues = \array_combine($new_issues, $new_issues);
Expand All @@ -589,10 +578,8 @@ public function addSuppressedIssues(array $new_issues)

/**
* @param array<int, string> $new_issues
*
* @return void
*/
public function removeSuppressedIssues(array $new_issues)
public function removeSuppressedIssues(array $new_issues): void
{
if (isset($new_issues[0])) {
$new_issues = \array_combine($new_issues, $new_issues);
Expand Down
Expand Up @@ -258,7 +258,7 @@ public static function getReturnTypes(
protected static function getYieldTypeFromExpression(
PhpParser\Node\Expr $stmt,
\Psalm\Internal\Provider\NodeDataProvider $nodes
) {
): array {
if ($stmt instanceof PhpParser\Node\Expr\Yield_) {
$key_type = null;

Expand Down
8 changes: 2 additions & 6 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -1857,10 +1857,8 @@ public function getSuppressedIssues(): array

/**
* @param array<int, string> $new_issues
*
* @return void
*/
public function addSuppressedIssues(array $new_issues)
public function addSuppressedIssues(array $new_issues): void
{
if (isset($new_issues[0])) {
$new_issues = \array_combine($new_issues, $new_issues);
Expand All @@ -1871,10 +1869,8 @@ public function addSuppressedIssues(array $new_issues)

/**
* @param array<int, string> $new_issues
*
* @return void
*/
public function removeSuppressedIssues(array $new_issues)
public function removeSuppressedIssues(array $new_issues): void
{
if (isset($new_issues[0])) {
$new_issues = \array_combine($new_issues, $new_issues);
Expand Down
5 changes: 1 addition & 4 deletions src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php
Expand Up @@ -18,10 +18,7 @@ public function __construct(
parent::__construct($interface, $source, $fq_interface_name);
}

/**
* @return void
*/
public function analyze()
public function analyze(): void
{
if (!$this->class instanceof PhpParser\Node\Stmt\Interface_) {
throw new \LogicException('Something went badly wrong');
Expand Down
8 changes: 2 additions & 6 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -495,8 +495,7 @@ public function server($address = '127.0.0.1:12345', bool $socket_server_mode =
$reader = new ProtocolStreamReader($socket);
$reader->on(
'close',
/** @return void */
function () {
function (): void {
fwrite(STDOUT, "Connection closed\n");
}
);
Expand Down Expand Up @@ -1345,15 +1344,12 @@ public function getFileAnalyzerForClassLike(string $fq_class_name): FileAnalyzer
return $file_analyzer;
}

/**
* @return void
*/
public function getMethodMutations(
\Psalm\Internal\MethodIdentifier $original_method_id,
Context $this_context,
string $root_file_path,
string $root_file_name
) {
): void {
$fq_class_name = $original_method_id->fq_class_name;

$appearing_method_id = $this->codebase->methods->getAppearingMethodId($original_method_id);
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ScopeAnalyzer.php
Expand Up @@ -82,7 +82,7 @@ public static function getFinalControlActions(
array $exit_functions,
array $break_types = [],
$return_is_exit = true
) {
): array {
if (empty($stmts)) {
return [self::ACTION_NONE];
}
Expand Down

0 comments on commit 10f2966

Please sign in to comment.