2019-01-07 14:38:56 +01:00
|
|
|
|
<?php
|
|
|
|
|
namespace Psalm\Example\Plugin;
|
|
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
use Psalm\Checker;
|
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
|
use Psalm\FileManipulation;
|
2021-01-06 15:05:53 +01:00
|
|
|
|
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
|
|
|
|
|
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
|
2019-01-07 14:38:56 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prevents any assignment to a float value
|
|
|
|
|
*/
|
|
|
|
|
class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Called after an expression has been checked
|
|
|
|
|
*
|
|
|
|
|
* @return null|false
|
|
|
|
|
*/
|
2021-01-06 15:05:53 +01:00
|
|
|
|
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool {
|
|
|
|
|
$expr = $event->getExpr();
|
|
|
|
|
$statements_source = $event->getStatementsSource();
|
2019-01-07 14:38:56 +01:00
|
|
|
|
if ($expr instanceof PhpParser\Node\Expr\Assign
|
2019-11-25 17:27:53 +01:00
|
|
|
|
&& ($expr_type = $statements_source->getNodeTypeProvider()->getType($expr->expr))
|
2019-11-25 17:44:54 +01:00
|
|
|
|
&& $expr_type->hasFloat()
|
2019-01-07 14:38:56 +01:00
|
|
|
|
) {
|
|
|
|
|
if (\Psalm\IssueBuffer::accepts(
|
|
|
|
|
new NoFloatAssignment(
|
|
|
|
|
'Don’t assign to floats',
|
|
|
|
|
new CodeLocation($statements_source, $expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
|
|
return null;
|
2019-01-07 14:38:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NoFloatAssignment extends \Psalm\Issue\PluginIssue {
|
|
|
|
|
}
|