Skip to content
Permalink
Browse files

Add support for non-empty scalar

Ref #2449
  • Loading branch information
muglug committed Dec 9, 2019
1 parent 447e353 commit aaffdbc79f383f5077e9386fc7bc156564340d0e
@@ -1959,6 +1959,57 @@ private static function reconcileFalsyOrEmpty(
}
}

if ($existing_var_type->hasScalar()) {
if ($existing_var_type->isSingle()
&& $existing_var_atomic_types['scalar'] instanceof Type\Atomic\TNonEmptyScalar
) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new ParadoxicalCondition(
'Found a paradox when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a ' . $assertion . ' assertion',
$code_location
),
$suppressed_issues
)
) {
// fall through
}

return Type::getScalar();
}

if (!$existing_var_atomic_types['scalar'] instanceof Type\Atomic\TEmptyScalar) {
$did_remove_type = true;
$existing_var_type->removeType('scalar');

if (!$existing_var_atomic_types['scalar'] instanceof Type\Atomic\TNonEmptyScalar) {
$existing_var_type->addType(new Type\Atomic\TEmptyScalar);
}
} elseif ($existing_var_type->isSingle()) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new RedundantCondition(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a ' . $assertion . ' assertion',
$code_location
),
$suppressed_issues
)
) {
// fall through
}
}

if ($existing_var_type->isSingle()) {
return $existing_var_type;
}
}

if ($existing_var_type->hasType('bool')) {
$did_remove_type = true;
$existing_var_type->removeType('bool');
@@ -580,6 +580,14 @@ private static function reconcileFalsyOrEmpty(
}
}

if ($existing_var_type->hasScalar()) {
$existing_var_type->removeType('scalar');

if (!$existing_var_atomic_types['scalar'] instanceof Type\Atomic\TEmptyScalar) {
$existing_var_type->addType(new Type\Atomic\TNonEmptyScalar);
}
}

self::removeFalsyNegatedLiteralTypes(
$existing_var_type,
$did_remove_type
@@ -648,6 +656,36 @@ private static function reconcileFalsyOrEmpty(
}
}

if ($existing_var_type->hasScalar()) {
if (!$existing_var_atomic_types['scalar'] instanceof Type\Atomic\TNonEmptyScalar) {
$did_remove_type = true;
$existing_var_type->removeType('scalar');

if (!$existing_var_atomic_types['scalar'] instanceof Type\Atomic\TEmptyScalar) {
$existing_var_type->addType(new Type\Atomic\TNonEmptyScalar);
}
} elseif ($existing_var_type->isSingle() && !$is_equality) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new RedundantCondition(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location
),
$suppressed_issues
)
) {
// fall through
}
}

if ($existing_var_type->isSingle()) {
return $existing_var_type;
}
}

if ($existing_var_type->hasType('null')) {
$did_remove_type = true;
$existing_var_type->removeType('null');
@@ -1275,6 +1275,16 @@ public static function getMixed($from_loop_isset = false)
return new Union([$type]);
}

/**
* @return Type\Union
*/
public static function getScalar()
{
$type = new TMixed();

return new Union([$type]);
}

/**
* @return Type\Union
*/
@@ -0,0 +1,13 @@
<?php
namespace Psalm\Type\Atomic;

class TNonEmptyScalar extends TScalar
{
/**
* @return string
*/
public function getId()
{
return 'non-empty-scalar';
}
}
@@ -2200,6 +2200,19 @@ function foo(string $type, bool $and) : void {
}
}'
],
'allowEmptyScalarAndNonEmptyScalarAssertions' => [
'<?php
/** @param mixed $value */
function foo($value) : void {
if (\is_scalar($value)) {
if ($value) {
echo $value;
} else {
echo $value;
}
}
}'
],
];
}

@@ -2491,6 +2504,62 @@ function foo(?string $s) : string {
}',
'error_message' => 'TypeDoesNotContainType'
],
'allowEmptyScalarAndNonEmptyScalarAssertions1' => [
'<?php
/** @param mixed $value */
function foo($value) : void {
if (\is_scalar($value)) {
if ($value) {
if (\is_scalar($value)) {}
} else {
echo $value;
}
}
}',
'error_message' => 'RedundantCondition',
],
'allowEmptyScalarAndNonEmptyScalarAssertions2' => [
'<?php
/** @param mixed $value */
function foo($value) : void {
if (\is_scalar($value)) {
if ($value) {
echo $value;
} else {
if (\is_scalar($value)) {}
}
}
}',
'error_message' => 'RedundantCondition',
],
'allowEmptyScalarAndNonEmptyScalarAssertions3' => [
'<?php
/** @param mixed $value */
function foo($value) : void {
if (\is_scalar($value)) {
if ($value) {
if ($value) {}
} else {
echo $value;
}
}
}',
'error_message' => 'RedundantCondition',
],
'allowEmptyScalarAndNonEmptyScalarAssertions4' => [
'<?php
/** @param mixed $value */
function foo($value) : void {
if (\is_scalar($value)) {
if ($value) {
echo $value;
} else {
if (!$value) {}
}
}
}',
'error_message' => 'RedundantCondition',
],
];
}
}

0 comments on commit aaffdbc

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