Skip to content

Commit

Permalink
Merge pull request #6 from sasezaki/hotfix/boolean
Browse files Browse the repository at this point in the history
support bool
  • Loading branch information
sasezaki committed Aug 1, 2020
2 parents cfa8085 + af67889 commit 607ac80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -14,6 +14,12 @@ This is VERY VERY **Experimental** .
* NOT support global variables.
* NOT support variables in namespace.

##
```
$ composer require --dev struggle-for-php/sfp-psalm-typed-local-variable-plugin
$ vendor/bin/psalm-plugin enable struggle-for-php/sfp-psalm-typed-local-variable-plugin
```

## demo

```php
Expand Down
5 changes: 5 additions & 0 deletions src/AssignAnalyzer.php
Expand Up @@ -13,12 +13,15 @@
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Union;
use Sfp\Psalm\TypedLocalVariablePlugin\Issue\InvalidScalarTypedLocalVariableIssue;
use Sfp\Psalm\TypedLocalVariablePlugin\Issue\InvalidTypedLocalVariableIssue;
Expand Down Expand Up @@ -54,6 +57,8 @@ private static function rollupLiteral(Union $firstDeclare): Union
$types[] = new TInt();
} elseif ($atomicType instanceof TLiteralFloat) {
$types[] = new TFloat();
} elseif ($atomicType instanceof TFalse || $atomicType instanceof TTrue) {
$types[] = new TBool();
} else {
$types[] = $atomicType;
}
Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/TypedLocalVariableCheckerTest.php
Expand Up @@ -65,15 +65,19 @@ function func () : void {
$float = 0.2;
// $float = 1; // allowed
$float = false;
$bool = false;
$bool = true;
$bool = 1;
}
CODE
);
$this->analyzeFile(__METHOD__, new Context());

$this->assertSame(3, IssueBuffer::getErrorCount());
$this->assertSame(4, IssueBuffer::getErrorCount());
$this->assertSame('$string = false;', trim(current(IssueBuffer::getIssuesData())[0]->snippet));
$this->assertSame('$int = false;', trim(current(IssueBuffer::getIssuesData())[1]->snippet));
$this->assertSame('$float = false;', trim(current(IssueBuffer::getIssuesData())[2]->snippet));
$this->assertSame('$bool = 1;', trim(current(IssueBuffer::getIssuesData())[3]->snippet));
}

/**
Expand Down

0 comments on commit 607ac80

Please sign in to comment.