forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionAnalyzer.php
127 lines (104 loc) · 4.24 KB
/
FunctionAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Psalm\Internal\Analyzer;
use PhpParser;
use Psalm\Internal\Codebase\InternalCallMapHandler;
use Psalm\Context;
use Psalm\Type;
use function strtolower;
use function array_values;
use function count;
use function is_string;
/**
* @internal
*/
class FunctionAnalyzer extends FunctionLikeAnalyzer
{
/**
* @var PhpParser\Node\Stmt\Function_
*/
protected $function;
public function __construct(PhpParser\Node\Stmt\Function_ $function, SourceAnalyzer $source)
{
$codebase = $source->getCodebase();
$file_storage_provider = $codebase->file_storage_provider;
$file_storage = $file_storage_provider->get($source->getFilePath());
$namespace = $source->getNamespace();
$function_id = ($namespace ? strtolower($namespace) . '\\' : '') . strtolower($function->name->name);
if (!isset($file_storage->functions[$function_id])) {
throw new \UnexpectedValueException(
'Function ' . $function_id . ' should be defined in ' . $source->getFilePath()
);
}
$storage = $file_storage->functions[$function_id];
parent::__construct($function, $source, $storage);
}
/**
* @return non-empty-lowercase-string
*/
public function getFunctionId(): string
{
$namespace = $this->source->getNamespace();
/** @var non-empty-lowercase-string */
return ($namespace ? strtolower($namespace) . '\\' : '') . strtolower($this->function->name->name);
}
public static function analyzeStatement(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Stmt\Function_ $stmt,
Context $context
) : void {
foreach ($stmt->stmts as $function_stmt) {
if ($function_stmt instanceof PhpParser\Node\Stmt\Global_) {
foreach ($function_stmt->vars as $var) {
if ($var instanceof PhpParser\Node\Expr\Variable) {
if (is_string($var->name)) {
$var_id = '$' . $var->name;
// registers variable in global context
$context->hasVariable($var_id);
}
}
}
} elseif (!$function_stmt instanceof PhpParser\Node\Stmt\Nop) {
break;
}
}
$codebase = $statements_analyzer->getCodebase();
if (!$codebase->register_stub_files
&& !$codebase->register_autoload_files
) {
$function_name = strtolower($stmt->name->name);
if ($ns = $statements_analyzer->getNamespace()) {
$fq_function_name = strtolower($ns) . '\\' . $function_name;
} else {
$fq_function_name = $function_name;
}
$function_context = new Context($context->self);
$function_context->strict_types = $context->strict_types;
$config = \Psalm\Config::getInstance();
$function_context->collect_exceptions = $config->check_for_throws_docblock;
if ($function_analyzer = $statements_analyzer->getFunctionAnalyzer($fq_function_name)) {
$function_analyzer->analyze(
$function_context,
$statements_analyzer->node_data,
$context
);
if ($config->reportIssueInFile('InvalidReturnType', $statements_analyzer->getFilePath())) {
$method_id = $function_analyzer->getId();
$function_storage = $codebase->functions->getStorage(
$statements_analyzer,
strtolower($method_id)
);
$return_type = $function_storage->return_type;
$return_type_location = $function_storage->return_type_location;
$function_analyzer->verifyReturnType(
$stmt->getStmts(),
$statements_analyzer,
$return_type,
$statements_analyzer->getFQCLN(),
$return_type_location,
$function_context->has_returned
);
}
}
}
}
}