Skip to content

Commit

Permalink
Merge pull request #7137 from orklah/SingleAtomic
Browse files Browse the repository at this point in the history
fix some more expressions that can be replaced by getSingleAtomic
  • Loading branch information
orklah committed Dec 11, 2021
2 parents cc2e667 + 50cc629 commit b7a1528
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 33 deletions.
Expand Up @@ -34,7 +34,6 @@
use function array_pop;
use function array_values;
use function count;
use function current;
use function get_class;

class CastAnalyzer
Expand All @@ -61,9 +60,8 @@ public static function analyze(
}
}

$maybe = $maybe_type->getAtomicTypes();

if (count($maybe) === 1 && current($maybe) instanceof Type\Atomic\TBool) {
if (count($maybe_type->getAtomicTypes()) === 1
&& $maybe_type->getSingleAtomic() instanceof Type\Atomic\TBool) {
$as_int = false;
$type = new Type\Union([
new Type\Atomic\TLiteralInt(0),
Expand Down
Expand Up @@ -18,12 +18,10 @@
use ReflectionProperty;

use function array_merge;
use function array_shift;
use function array_values;
use function count;
use function is_string;
use function preg_match;
use function reset;
use function strtolower;

use const PHP_INT_MAX;
Expand Down Expand Up @@ -77,20 +75,12 @@ public static function infer(
return Type::getString($result);
}

if ($left->isString()) {
$left_string_types = $left->getAtomicTypes();
$left_string_type = reset($left_string_types);
if ($left_string_type instanceof Type\Atomic\TNonEmptyString) {
return new Type\Union([new Type\Atomic\TNonEmptyString()]);
}
if ($left->isSingle() && $left->getSingleAtomic() instanceof Type\Atomic\TNonEmptyString) {
return new Type\Union([new Type\Atomic\TNonEmptyString()]);
}

if ($right->isString()) {
$right_string_types = $right->getAtomicTypes();
$right_string_type = reset($right_string_types);
if ($right_string_type instanceof Type\Atomic\TNonEmptyString) {
return new Type\Union([new Type\Atomic\TNonEmptyString()]);
}
if ($right->isSingle() && $right->getSingleAtomic() instanceof Type\Atomic\TNonEmptyString) {
return new Type\Union([new Type\Atomic\TNonEmptyString()]);
}
}

Expand Down Expand Up @@ -639,15 +629,13 @@ private static function handleArrayItem(
return false;
}

$dim_atomic_types = $dim_type->getAtomicTypes();

if (count($dim_atomic_types) > 1
if (count($dim_type->getAtomicTypes()) > 1
|| $dim_type->hasMixed()
|| count($array_creation_info->property_types) > 50
) {
$array_creation_info->can_create_objectlike = false;
} else {
$atomic_type = array_shift($dim_atomic_types);
$atomic_type = $dim_type->getSingleAtomic();

if ($atomic_type instanceof Type\Atomic\TLiteralInt
|| $atomic_type instanceof Type\Atomic\TLiteralString
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php
Expand Up @@ -38,7 +38,6 @@

use function array_merge;
use function count;
use function current;
use function explode;
use function reset;
use function strtolower;
Expand Down Expand Up @@ -606,7 +605,7 @@ private static function potentiallyInferTypesOnClosureFromParentReturnType(
}

/** @var Type\Atomic\TClosure|Type\Atomic\TCallable $parent_callable_return_type */
$parent_callable_return_type = current($parent_fn_storage->return_type->getAtomicTypes());
$parent_callable_return_type = $parent_fn_storage->return_type->getSingleAtomic();

if ($parent_callable_return_type->params === null && $parent_callable_return_type->return_type === null) {
return;
Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/Internal/PhpVisitor/Reflector/TypeHintResolver.php
Expand Up @@ -11,7 +11,6 @@
use UnexpectedValueException;

use function implode;
use function reset;
use function strtolower;

class TypeHintResolver
Expand Down Expand Up @@ -96,8 +95,7 @@ public static function resolve(
);

if ($type_string) {
$atomic_types = $type->getAtomicTypes();
$atomic_type = reset($atomic_types);
$atomic_type = $type->getSingleAtomic();
$atomic_type->text = $type_string;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Expand Up @@ -947,11 +947,9 @@ public static function handleTemplateParamClassStandin(
$depth + 1
);

$as_type_union_types = $as_type_union->getAtomicTypes();
$first = $as_type_union->getSingleAtomic();

$first = reset($as_type_union_types);

if (count($as_type_union_types) === 1 && $first instanceof Atomic\TNamedObject) {
if (count($as_type_union->getAtomicTypes()) === 1 && $first instanceof Atomic\TNamedObject) {
$atomic_type->as_type = $first;
} else {
$atomic_type->as_type = null;
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -406,8 +406,7 @@ private static function getGenericParamClass(
}

if ($t instanceof Atomic\TTemplateParam) {
$t_atomic_types = $t->as->getAtomicTypes();
$t_atomic_type = count($t_atomic_types) === 1 ? reset($t_atomic_types) : null;
$t_atomic_type = count($t->as->getAtomicTypes()) === 1 ? $t->as->getSingleAtomic() : null;

if (!$t_atomic_type instanceof TNamedObject) {
$t_atomic_type = null;
Expand Down

0 comments on commit b7a1528

Please sign in to comment.