Skip to content

Commit

Permalink
Merge pull request #3 from danog/main
Browse files Browse the repository at this point in the history
Switch to psalm v5
  • Loading branch information
orklah committed Dec 2, 2022
2 parents cf6161f + c4b11a5 commit 81c2487
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require": {
"php": "^7.3|^8.0",
"vimeo/psalm": "^4.6.1"
"vimeo/psalm": "^5"
},
"autoload": {
"psr-4": {
Expand All @@ -25,6 +25,6 @@
"require-dev": {
"nikic/php-parser": "^4.0",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.15.0"
"psalm/plugin-phpunit": "^0.18.0"
}
}
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
25 changes: 15 additions & 10 deletions src/Hooks/NotEmptyHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NotEmptyHooks implements AfterExpressionAnalysisInterface
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
if (!$event->getCodebase()->alter_code) {
return true;
return null;
}

$original_expr = $event->getExpr();
Expand All @@ -34,38 +34,43 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
} elseif ($original_expr instanceof Empty_) {
if ($event->getContext()->inside_negation) {
//we're inside a negation. If we start messing with replacements now, we won't be able to handle the negation then
return true;
return null;
}
$expr = $original_expr;
} else {
return true;
return null;
}

if (!$expr->expr instanceof Variable) {
return true;
return null;
}

$type = $node_provider->getType($expr->expr);
if ($type === null) {
return true;
return null;
}

if ($type->from_docblock) {
//TODO: maybe add an issue in non alter mode
return true;
return null;
}

if (!is_string($expr->expr->name)) {
return null;
}

$display_expr = '$' . $expr->expr->name;

$replacement = [];
if ($type->isSingleAndMaybeNullable() && $type->isNullable()) {
$replacement[] = $display_expr . ' ' . $comparison_operator . ' ' . 'null';
$type = $type->getBuilder();
$type->removeType('null');
}

if(!$type->isSingle()){
//we removed null but the type is still not single
return true;
return null;
}

$atomic_types = $type->getAtomicTypes();
Expand Down Expand Up @@ -100,14 +105,14 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
$replacement[] = 'true';
}
} else {
return true;
return null;
}

if ($replacement !== []) {
$replacement = array_unique($replacement); // deduplicate some conditions (null from type and from maybe nullable)
if (count($replacement) > 2) {
//at one point, you have to ask if empty is not the best thing to use!
return true;
return null;
}

$replacement_string = implode(' ' . $combination_operator . ' ', $replacement);
Expand All @@ -121,6 +126,6 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
$event->setFileReplacements([$file_manipulation]);
}

return true;
return null;
}
}

0 comments on commit 81c2487

Please sign in to comment.