Skip to content

Commit c60ca9d

Browse files
committed
Check whether function has return typehint
1 parent c00b30d commit c60ca9d

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-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
}

Diff for: rules.neon

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rules:
1515
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
1616
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
1717
- PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule
18+
- PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule
1819
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule
1920
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
2021
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
+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+
30,
26+
],
27+
]);
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
$closure = function($a, $b, $c) {
13+
14+
};
15+
16+
return false;
17+
}
18+
19+
/**
20+
* @return bool
21+
*/
22+
function globalFunction3($a, $b, $c)
23+
{
24+
return false;
25+
}
26+
}
27+
28+
namespace MissingFunctionReturnTypehint
29+
{
30+
function namespacedFunction1($d, $e)
31+
{
32+
return 9;
33+
};
34+
35+
function namespacedFunction2($d, $e): int
36+
{
37+
return 9;
38+
};
39+
40+
/**
41+
* @return int
42+
*/
43+
function namespacedFunction3($d, $e)
44+
{
45+
return 9;
46+
};
47+
}

0 commit comments

Comments
 (0)