1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/examples/plugins/FunctionCasingChecker.php

115 lines
3.9 KiB
PHP
Raw Normal View History

2019-01-07 15:34:16 +01:00
<?php
namespace Psalm\Example\Plugin;
use PhpParser;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Psalm\Checker;
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\Plugin\EventHandler\AfterFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterFunctionCallAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use function explode;
use function strtolower;
use function end;
2019-01-07 15:34:16 +01:00
/**
* Prevents any assignment to a float value
*/
class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, AfterMethodCallAnalysisInterface
{
/**
* @param MethodCall|StaticCall $expr
* @param FileManipulation[] $file_replacements
*/
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
$expr = $event->getExpr();
$codebase = $event->getCodebase();
$declaring_method_id = $event->getDeclaringMethodId();
$statements_source = $event->getStatementsSource();
2019-01-07 15:34:16 +01:00
if (!$expr->name instanceof PhpParser\Node\Identifier) {
return;
}
try {
2020-10-15 00:51:15 +02:00
/** @psalm-suppress ArgumentTypeCoercion */
$method_id = new \Psalm\Internal\MethodIdentifier(...explode('::', $declaring_method_id));
$function_storage = $codebase->methods->getStorage($method_id);
2019-01-07 15:34:16 +01:00
if ($function_storage->cased_name === '__call') {
return;
}
if ($function_storage->cased_name === '__callStatic') {
return;
}
2019-01-07 15:34:16 +01:00
if ($function_storage->cased_name !== (string)$expr->name) {
if (\Psalm\IssueBuffer::accepts(
new IncorrectFunctionCasing(
'Function is incorrectly cased, expecting ' . $function_storage->cased_name,
new CodeLocation($statements_source, $expr->name)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
}
} catch (\Exception $e) {
// can throw if storage is missing
}
}
/**
2020-05-15 16:18:05 +02:00
* @param non-empty-string $function_id
2019-01-07 15:34:16 +01:00
* @param FileManipulation[] $file_replacements
*/
public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $event): void
{
$expr = $event->getExpr();
$codebase = $event->getCodebase();
$statements_source = $event->getStatementsSource();
$function_id = $event->getFunctionId();
2019-03-16 17:34:48 +01:00
if ($expr->name instanceof PhpParser\Node\Expr) {
2019-01-07 15:34:16 +01:00
return;
}
try {
$function_storage = $codebase->functions->getStorage(
$statements_source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer
? $statements_source
: null,
strtolower($function_id)
2019-01-07 15:34:16 +01:00
);
2019-11-25 21:20:31 +01:00
if (!$function_storage->cased_name) {
return;
}
2019-03-16 17:34:48 +01:00
$function_name_parts = explode('\\', $function_storage->cased_name);
if (end($function_name_parts) !== end($expr->name->parts)) {
2019-01-07 15:34:16 +01:00
if (\Psalm\IssueBuffer::accepts(
new IncorrectFunctionCasing(
'Function is incorrectly cased, expecting ' . $function_storage->cased_name,
new CodeLocation($statements_source, $expr->name)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
}
} catch (\Exception $e) {
// can throw if storage is missing
}
}
}
class IncorrectFunctionCasing extends \Psalm\Issue\PluginIssue
{
2019-01-07 15:34:16 +01:00
}