2019-01-07 14:38:56 +01:00
|
|
|
|
<?php
|
2022-12-17 05:00:34 +01:00
|
|
|
|
|
2019-01-07 14:38:56 +01:00
|
|
|
|
namespace Psalm\Example\Plugin;
|
|
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
use Psalm\CodeLocation;
|
2021-12-27 20:18:33 +01:00
|
|
|
|
use Psalm\Issue\PluginIssue;
|
|
|
|
|
use Psalm\IssueBuffer;
|
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
|
|
|
|
|
*/
|
2023-10-26 17:00:29 +02:00
|
|
|
|
final class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface
|
2019-01-07 14:38:56 +01:00
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Called after an expression has been checked
|
2022-12-17 15:13:32 +01:00
|
|
|
|
*
|
|
|
|
|
* @return null
|
2019-01-07 14:38:56 +01:00
|
|
|
|
*/
|
2022-12-18 17:15:15 +01:00
|
|
|
|
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
|
|
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
|
$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
|
|
|
|
) {
|
2022-12-17 05:00:34 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-01-07 14:38:56 +01:00
|
|
|
|
new NoFloatAssignment(
|
|
|
|
|
'Don’t assign to floats',
|
2022-12-18 17:15:15 +01:00
|
|
|
|
new CodeLocation($statements_source, $expr),
|
2019-01-07 14:38:56 +01:00
|
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
|
$statements_source->getSuppressedIssues(),
|
2022-12-17 05:00:34 +01:00
|
|
|
|
);
|
2019-01-07 14:38:56 +01:00
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
|
|
return null;
|
2019-01-07 14:38:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:00:29 +02:00
|
|
|
|
final class NoFloatAssignment extends PluginIssue
|
2022-12-18 17:15:15 +01:00
|
|
|
|
{
|
2019-01-07 14:38:56 +01:00
|
|
|
|
}
|