Skip to content

Commit 884029d

Browse files
committed
Check whether function has return typehint
1 parent c94d4bf commit 884029d

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"autoload-dev": {
3131
"classmap": ["tests/"],
3232
"files": [
33-
"tests/Rules/Functions/data/missing-function-parameter-typehint.php"
33+
"tests/Rules/Functions/data/missing-function-parameter-typehint.php",
34+
"tests/Rules/Functions/data/missing-function-return-typehint.php"
3435
]
3536
}
3637
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Functions;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Broker\Broker;
8+
use PHPStan\Reflection\BrokerAwareExtension;
9+
use PHPStan\Type\MixedType;
10+
11+
final class MissingFunctionReturnTypehintRule implements \PHPStan\Rules\Rule, BrokerAwareExtension
12+
{
13+
14+
/** @var Broker */
15+
private $broker;
16+
17+
/**
18+
* @return string Class implementing \PhpParser\Node
19+
*/
20+
public function getNodeType(): string
21+
{
22+
return \PhpParser\Node\Stmt\Function_::class;
23+
}
24+
25+
/**
26+
* @param \PhpParser\Node\Stmt\Function_ $node
27+
* @param \PHPStan\Analyser\Scope $scope
28+
*
29+
* @return string[] errors
30+
*/
31+
public function processNode(Node $node, Scope $scope): array
32+
{
33+
if ($scope->isInClass()) {
34+
throw new \PHPStan\ShouldNotHappenException();
35+
}
36+
37+
$functionReflection = $this->broker->getCustomFunction(new Node\Name($node->name, $node->getAttributes()), $scope);
38+
$returnType = $functionReflection->getReturnType();
39+
40+
if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
41+
return [
42+
sprintf(
43+
'Function %s() has no return typehint specified',
44+
$functionReflection->getName()
45+
),
46+
];
47+
}
48+
49+
return [];
50+
}
51+
52+
public function setBroker(Broker $broker): void
53+
{
54+
$this->broker = $broker;
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Functions;
4+
5+
class MissingFunctionReturnTypehintRuleTest extends \PHPStan\Testing\RuleTestCase
6+
{
7+
8+
protected function getRule(): \PHPStan\Rules\Rule
9+
{
10+
$rule = new MissingFunctionReturnTypehintRule();
11+
$rule->setBroker($this->createBroker([], []));
12+
13+
return $rule;
14+
}
15+
16+
public function testRule(): void
17+
{
18+
$this->analyse([__DIR__ . '/data/missing-function-return-typehint.php'], [
19+
[
20+
'Function globalFunction1() has no return typehint specified',
21+
5,
22+
],
23+
[
24+
'Function MissingFunctionReturnTypehint\namespacedFunction1() has no return typehint specified',
25+
26,
26+
],
27+
]);
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace
4+
{
5+
function globalFunction1($a, $b, $c)
6+
{
7+
return false;
8+
}
9+
10+
function globalFunction2($a, $b, $c): bool
11+
{
12+
return false;
13+
}
14+
15+
/**
16+
* @return bool
17+
*/
18+
function globalFunction3($a, $b, $c)
19+
{
20+
return false;
21+
}
22+
}
23+
24+
namespace MissingFunctionReturnTypehint
25+
{
26+
function namespacedFunction1($d, $e)
27+
{
28+
return 9;
29+
};
30+
31+
function namespacedFunction2($d, $e): int
32+
{
33+
return 9;
34+
};
35+
36+
/**
37+
* @return int
38+
*/
39+
function namespacedFunction3($d, $e)
40+
{
41+
return 9;
42+
};
43+
}

0 commit comments

Comments
 (0)