Skip to content

Commit

Permalink
Fix issues Psalm identified
Browse files Browse the repository at this point in the history
1. Support (and ignore) first-class callables for `Mockery::mock(...)`
   and `Mockery::spy(...)`
2. Fix prefix and bracket logic
3. Drop unused `@var`
4. Force `resolvedName` to be treated as `class-string` (because it is
   a class-string)
  • Loading branch information
weirdan committed Nov 25, 2022
1 parent 1057355 commit 712eac2
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions Hooks/MockReturnTypeUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Psalm\MockeryPlugin\Hooks;

use PhpParser;
use PhpParser\Node\Arg;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Type;
Expand All @@ -17,7 +18,11 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
$return_type_candidate = $event->getReturnTypeCandidate();
$expr = $event->getExpr();
$method_id = $event->getMethodId();
if ($return_type_candidate && self::isMockMethod($method_id) && isset($expr->args[0])) {
if ($return_type_candidate
&& self::isMockMethod($method_id)
&& isset($expr->args[0])
&& $expr->args[0] instanceof Arg
) {
$first_arg = $expr->args[0]->value;

$fq_class_name = null;
Expand All @@ -27,6 +32,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
&& $first_arg->name instanceof PhpParser\Node\Identifier
&& $first_arg->name->name === 'class'
) {
/** @var class-string */
$fq_class_name = $first_arg->class->getAttribute('resolvedName');
} elseif (
$first_arg instanceof PhpParser\Node\Expr\BinaryOp\Concat
Expand All @@ -36,22 +42,23 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
&& $first_arg->right instanceof PhpParser\Node\Scalar\String_
&& $first_arg->right->value[0] === '['
) {
/** @var PhpParser\Node\Expr\ClassConstFetch $left */
$left = $first_arg->left;
/** @var class-string */
$fq_class_name = $left->class->getAttribute('resolvedName');
} elseif ($first_arg instanceof PhpParser\Node\Scalar\String_) {
if (substr($first_arg->value, 0, 6) === 'alias:') {
$trimmed_value = substr($first_arg->value, 6);
$value = $first_arg->value;
if (substr($value, 0, 6) === 'alias:') {
$value = substr($value, 6);
} elseif (substr($first_arg->value, 0, 9) === 'overload:') {
$trimmed_value = substr($first_arg->value, 9);
$value = substr($value, 9);
}

$bracket_position = strpos($first_arg->value, '[');
$fq_class_name = substr(
(!isset($trimmed_value) || $trimmed_value === false) ? $first_arg->value : $trimmed_value,
0,
$bracket_position === false ? strlen($first_arg->value) : $bracket_position
);
$bracket_position = strpos($value, '[');
if ($bracket_position !== false) {
$fq_class_name = substr($value, 0, $bracket_position);
} else {
$fq_class_name = $value;
}
}

if ($fq_class_name) {
Expand Down

0 comments on commit 712eac2

Please sign in to comment.