Skip to content

Check whether function parameter+return has typehint #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
}
},
"autoload-dev": {
"classmap": ["tests/"]
"classmap": ["tests/"],
"files": [
"tests/Rules/Functions/data/missing-function-parameter-typehint.php",
"tests/Rules/Functions/data/missing-function-return-typehint.php"
]
}
}
2 changes: 2 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ rules:
- PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
- PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule
- PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
Expand Down
67 changes: 67 additions & 0 deletions src/Rules/Functions/MissingFunctionParameterTypehintRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Type\MixedType;

final class MissingFunctionParameterTypehintRule implements \PHPStan\Rules\Rule
{

/** @var Broker */
private $broker;

public function __construct(Broker $broker)
{
$this->broker = $broker;
}

public function getNodeType(): string
{
return \PhpParser\Node\Stmt\Function_::class;
}

/**
* @param \PhpParser\Node\Stmt\Function_ $node
* @param \PHPStan\Analyser\Scope $scope
*
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$functionReflection = $this->broker->getCustomFunction(new Node\Name($node->name), $scope);

$messages = [];

foreach ($functionReflection->getParameters() as $parameterReflection) {
$message = $this->checkFunctionParameter($functionReflection, $parameterReflection);
if ($message === null) {
continue;
}

$messages[] = $message;
}

return $messages;
}

private function checkFunctionParameter(FunctionReflection $functionReflection, ParameterReflection $parameterReflection): ?string
{
$parameterType = $parameterReflection->getType();

if ($parameterType instanceof MixedType && !$parameterType->isExplicitMixed()) {
return sprintf(
'Function %s() has parameter $%s with no typehint specified.',
$functionReflection->getName(),
$parameterReflection->getName()
);
}

return null;
}

}
49 changes: 49 additions & 0 deletions src/Rules/Functions/MissingFunctionReturnTypehintRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Type\MixedType;

final class MissingFunctionReturnTypehintRule implements \PHPStan\Rules\Rule
{

/** @var Broker */
private $broker;

public function __construct(Broker $broker)
{
$this->broker = $broker;
}

public function getNodeType(): string
{
return \PhpParser\Node\Stmt\Function_::class;
}

/**
* @param \PhpParser\Node\Stmt\Function_ $node
* @param \PHPStan\Analyser\Scope $scope
*
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$functionReflection = $this->broker->getCustomFunction(new Node\Name($node->name), $scope);
$returnType = $functionReflection->getReturnType();

if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
return [
sprintf(
'Function %s() has no return typehint specified.',
$functionReflection->getName()
),
];
}

return [];
}

}
33 changes: 33 additions & 0 deletions tests/Rules/Functions/MissingFunctionParameterTypehintRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

class MissingFunctionParameterTypehintRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
$rule = new MissingFunctionParameterTypehintRule($this->createBroker([], []));

return $rule;
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/missing-function-parameter-typehint.php'], [
[
'Function globalFunction() has parameter $b with no typehint specified',
9,
],
[
'Function globalFunction() has parameter $c with no typehint specified',
9,
],
[
'Function MissingFunctionParameterTypehint\namespacedFunction() has parameter $d with no typehint specified',
24,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/Rules/Functions/MissingFunctionReturnTypehintRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

class MissingFunctionReturnTypehintRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
$rule = new MissingFunctionReturnTypehintRule($this->createBroker([], []));

return $rule;
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/missing-function-return-typehint.php'], [
[
'Function globalFunction1() has no return typehint specified',
5,
],
[
'Function MissingFunctionReturnTypehint\namespacedFunction1() has no return typehint specified',
30,
],
]);
}

}
27 changes: 27 additions & 0 deletions tests/Rules/Functions/data/missing-function-parameter-typehint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace
{
/**
* @param int $a
* @param $b
*/
function globalFunction($a, $b, $c): bool
{
$closure = function($a, $b, $c) {

};

return false;
}
}

namespace MissingFunctionParameterTypehint
{
/**
* @param $d
*/
function namespacedFunction($d, bool $e): int {
return 9;
};
}
47 changes: 47 additions & 0 deletions tests/Rules/Functions/data/missing-function-return-typehint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace
{
function globalFunction1($a, $b, $c)
{
return false;
}

function globalFunction2($a, $b, $c): bool
{
$closure = function($a, $b, $c) {

};

return false;
}

/**
* @return bool
*/
function globalFunction3($a, $b, $c)
{
return false;
}
}

namespace MissingFunctionReturnTypehint
{
function namespacedFunction1($d, $e)
{
return 9;
};

function namespacedFunction2($d, $e): int
{
return 9;
};

/**
* @return int
*/
function namespacedFunction3($d, $e)
{
return 9;
};
}