Skip to content

Instantly share code, notes, and snippets.

@muglug
Created January 7, 2019 12:40
Show Gist options
  • Save muglug/aee9a73156e458b058c15e623215c4d4 to your computer and use it in GitHub Desktop.
Save muglug/aee9a73156e458b058c15e623215c4d4 to your computer and use it in GitHub Desktop.
<?php
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\Checker;
use Psalm\Checker\StatementsChecker;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\IssueBuffer;
use Psalm\Issue\TypeCoercion;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\StatementsSource;
/**
* Prevents any assignment to a float value
*/
class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface
{
/**
* Called after an expression has been checked
*
* @param PhpParser\Node\Expr $expr
* @param Context $context
* @param StatementsSource $file_soure
* @param string[] $suppressed_issues
* @param FileManipulation[] $file_replacements
*
* @return null|false
*/
public static function afterExpressionAnalysis(
PhpParser\Node\Expr $expr,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
if ($expr instanceof PhpParser\Node\Expr\Assign
&& isset($expr->inferredType)
&& $expr->inferredType->hasFloat()
) {
if (\Psalm\IssueBuffer::accepts(
new \Psalm\Issue\InvalidArgument(
'Don’t assign to floats',
new CodeLocation($statements_source, $expr)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment