Skip to content
Permalink
Browse files

Fix #1591 - allow variable typed to string to be compared to literal …

…in reverse order
  • Loading branch information...
muglug committed May 2, 2019
1 parent fbe10c1 commit ae531506de5eba8beb8afbbd3a5b379faadcec85
@@ -2128,13 +2128,22 @@ protected static function hasReconcilableNonEmptyCountEqualityCheck(PhpParser\No
protected static function hasTypedValueComparison(PhpParser\Node\Expr\BinaryOp $conditional)
{
if (isset($conditional->right->inferredType)
&& ((!$conditional->right instanceof PhpParser\Node\Expr\Variable
&& !$conditional->right instanceof PhpParser\Node\Expr\PropertyFetch
&& !$conditional->right instanceof PhpParser\Node\Expr\StaticPropertyFetch)
|| $conditional->left instanceof PhpParser\Node\Expr\Variable
|| $conditional->left instanceof PhpParser\Node\Expr\PropertyFetch
|| $conditional->left instanceof PhpParser\Node\Expr\StaticPropertyFetch)
&& count($conditional->right->inferredType->getTypes()) === 1
&& !$conditional->right->inferredType->hasMixed()
) {
return self::ASSIGNMENT_TO_RIGHT;
}
if (isset($conditional->left->inferredType)
&& !$conditional->left instanceof PhpParser\Node\Expr\Variable
&& !$conditional->left instanceof PhpParser\Node\Expr\PropertyFetch
&& !$conditional->left instanceof PhpParser\Node\Expr\StaticPropertyFetch
&& count($conditional->left->inferredType->getTypes()) === 1
&& !$conditional->left->inferredType->hasMixed()
) {
@@ -2625,6 +2625,35 @@ private function __construct($value)
}',
'error_message' => 'InvalidDocblock',
],
'noComparisonToEmpty' => [
'<?php
/**
* @template K
* @template V
*/
class Container {
/** @var array<K, V> */
private $c;
/** @param array<K, V> $c */
public function __construct(array $c) {
$this->c = $c;
}
}
class Test {
/**
* @var Container<int, DateTime>
*/
private $c;
public function __construct()
{
$this->c = new Container([]);
}
}',
'error_message' => 'InvalidPropertyAssignmentValue'
],
];
}
}
@@ -567,6 +567,46 @@ function foo($s) : void {
'$b' => 'string',
],
],
'yodaConditionalsShouldHaveSameOutput1' => [
'<?php
class Foo {
/**
* @var array{from:bool, to:bool}
*/
protected $things = ["from" => false, "to" => false];
public function foo(string ...$things) : void {
foreach ($things as $thing) {
if ($thing !== "from" && $thing !== "to") {
continue;
}
$this->things[$thing] = !$this->things[$thing];
}
}
}
',
],
'yodaConditionalsShouldHaveSameOutput2' => [
'<?php
class Foo {
/**
* @var array{from:bool, to:bool}
*/
protected $things = ["from" => false, "to" => false];
public function foo(string ...$things) : void {
foreach ($things as $thing) {
if ("from" !== $thing && "to" !== $thing) {
continue;
}
$this->things[$thing] = !$this->things[$thing];
}
}
}
',
],
];
}

0 comments on commit ae53150

Please sign in to comment.
You can’t perform that action at this time.