Skip to content

Commit

Permalink
Moved new methods to StaticTypeFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 12, 2020
1 parent 69958e2 commit 5b88a8b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 58 deletions.
5 changes: 3 additions & 2 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -38,6 +38,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
Expand Down Expand Up @@ -652,10 +653,10 @@ public function specifyTypesInCondition(
private function handleDefaultTruthyOrFalseyContext(TypeSpecifierContext $context, Expr $expr): SpecifiedTypes
{
if (!$context->truthy()) {
$type = TypeUtils::truthy();
$type = StaticTypeFactory::truthy();
return $this->create($expr, $type, TypeSpecifierContext::createFalse());
} elseif (!$context->falsey()) {
$type = TypeUtils::falsey();
$type = StaticTypeFactory::falsey();
return $this->create($expr, $type, TypeSpecifierContext::createFalse());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/MixedType.php
Expand Up @@ -262,7 +262,7 @@ function () use ($level): string {

public function toBoolean(): BooleanType
{
if ($this->subtractedType !== null && TypeUtils::falsey()->equals($this->subtractedType)) {
if ($this->subtractedType !== null && StaticTypeFactory::falsey()->equals($this->subtractedType)) {
return new ConstantBooleanType(true);
}

Expand Down
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

public function removeFalsey(Type $type): Type
{
$falseyTypes = TypeUtils::falsey();
$falseyTypes = StaticTypeFactory::falsey();

if ($type instanceof ConstantArrayType) {
$keys = $type->getKeyTypes();
Expand Down
44 changes: 44 additions & 0 deletions src/Type/StaticTypeFactory.php
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;

class StaticTypeFactory
{

public static function falsey(): Type
{
static $falsey;

if ($falsey === null) {
$falsey = new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantStringType('0'),
new ConstantArrayType([], []),
]);
}

return $falsey;
}

public static function truthy(): Type
{
static $truthy;

if ($truthy === null) {
$truthy = new MixedType(false, self::falsey());
}

return $truthy;
}

}
33 changes: 0 additions & 33 deletions src/Type/TypeUtils.php
Expand Up @@ -5,9 +5,6 @@
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;

class TypeUtils
Expand Down Expand Up @@ -342,34 +339,4 @@ public static function containsCallable(Type $type): bool
return false;
}

public static function falsey(): Type
{
static $falsey;

if ($falsey === null) {
$falsey = new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantStringType('0'),
new ConstantArrayType([], []),
]);
}

return $falsey;
}

public static function truthy(): Type
{
static $truthy;

if ($truthy === null) {
$truthy = new MixedType(false, self::falsey());
}

return $truthy;
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Type/ArrayTypeTest.php
Expand Up @@ -53,7 +53,7 @@ public function dataIsSuperTypeOf(): array
TrinaryLogic::createYes(),
],
[
new ArrayType(new MixedType(), new MixedType(false, TypeUtils::falsey())),
new ArrayType(new MixedType(), new MixedType(false, StaticTypeFactory::falsey())),
new ConstantArrayType([], []),
TrinaryLogic::createYes(),
],
Expand Down
40 changes: 20 additions & 20 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Expand Up @@ -1730,24 +1730,24 @@ public function dataUnion(): array
],
[
[
TypeUtils::falsey(),
TypeUtils::falsey(),
StaticTypeFactory::falsey(),
StaticTypeFactory::falsey(),
],
UnionType::class,
'0|0.0|\'\'|\'0\'|array()|false|null',
],
[
[
TypeUtils::truthy(),
TypeUtils::truthy(),
StaticTypeFactory::truthy(),
StaticTypeFactory::truthy(),
],
MixedType::class,
'mixed~0|0.0|\'\'|\'0\'|array()|false|null=implicit',
],
[
[
TypeUtils::falsey(),
TypeUtils::truthy(),
StaticTypeFactory::falsey(),
StaticTypeFactory::truthy(),
],
MixedType::class,
'mixed=implicit',
Expand Down Expand Up @@ -1932,24 +1932,24 @@ public function dataIntersect(): array
],
[
[
TypeUtils::truthy(),
StaticTypeFactory::truthy(),
new BooleanType(),
],
ConstantBooleanType::class,
'true',
],
[
[
TypeUtils::falsey(),
StaticTypeFactory::falsey(),
new BooleanType(),
],
ConstantBooleanType::class,
'false',
],
[
[
TypeUtils::falsey(),
TypeUtils::truthy(),
StaticTypeFactory::falsey(),
StaticTypeFactory::truthy(),
],
NeverType::class,
'*NEVER*',
Expand Down Expand Up @@ -3029,38 +3029,38 @@ public function dataRemove(): array
'*NEVER*',
],
[
TypeUtils::falsey(),
TypeUtils::falsey(),
StaticTypeFactory::falsey(),
StaticTypeFactory::falsey(),
NeverType::class,
'*NEVER*',
],
[
TypeUtils::truthy(),
TypeUtils::truthy(),
StaticTypeFactory::truthy(),
StaticTypeFactory::truthy(),
NeverType::class,
'*NEVER*',
],
[
TypeUtils::truthy(),
TypeUtils::falsey(),
StaticTypeFactory::truthy(),
StaticTypeFactory::falsey(),
MixedType::class,
'mixed~0|0.0|\'\'|\'0\'|array()|false|null',
],
[
TypeUtils::falsey(),
TypeUtils::truthy(),
StaticTypeFactory::falsey(),
StaticTypeFactory::truthy(),
UnionType::class,
'0|0.0|\'\'|\'0\'|array()|false|null',
],
[
new BooleanType(),
TypeUtils::falsey(),
StaticTypeFactory::falsey(),
ConstantBooleanType::class,
'true',
],
[
new BooleanType(),
TypeUtils::truthy(),
StaticTypeFactory::truthy(),
ConstantBooleanType::class,
'false',
],
Expand Down

0 comments on commit 5b88a8b

Please sign in to comment.