Skip to content

Commit 0da3eb4

Browse files
committed
Moved new methods to StaticTypeFactory
1 parent 0dfd0d0 commit 0da3eb4

File tree

7 files changed

+71
-58
lines changed

7 files changed

+71
-58
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use PHPStan\Type\ObjectType;
3939
use PHPStan\Type\ObjectWithoutClassType;
4040
use PHPStan\Type\StaticType;
41+
use PHPStan\Type\StaticTypeFactory;
4142
use PHPStan\Type\Type;
4243
use PHPStan\Type\TypeCombinator;
4344
use PHPStan\Type\TypeTraverser;
@@ -652,10 +653,10 @@ public function specifyTypesInCondition(
652653
private function handleDefaultTruthyOrFalseyContext(TypeSpecifierContext $context, Expr $expr): SpecifiedTypes
653654
{
654655
if (!$context->truthy()) {
655-
$type = TypeUtils::truthy();
656+
$type = StaticTypeFactory::truthy();
656657
return $this->create($expr, $type, TypeSpecifierContext::createFalse());
657658
} elseif (!$context->falsey()) {
658-
$type = TypeUtils::falsey();
659+
$type = StaticTypeFactory::falsey();
659660
return $this->create($expr, $type, TypeSpecifierContext::createFalse());
660661
}
661662

src/Type/MixedType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function () use ($level): string {
262262

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

src/Type/Php/ArrayFilterFunctionReturnTypeReturnTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Type\MixedType;
1616
use PHPStan\Type\NeverType;
1717
use PHPStan\Type\NullType;
18+
use PHPStan\Type\StaticTypeFactory;
1819
use PHPStan\Type\Type;
1920
use PHPStan\Type\TypeCombinator;
2021
use PHPStan\Type\TypeUtils;
@@ -77,7 +78,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7778

7879
public function removeFalsey(Type $type): Type
7980
{
80-
$falseyTypes = TypeUtils::falsey();
81+
$falseyTypes = StaticTypeFactory::falsey();
8182

8283
if ($type instanceof ConstantArrayType) {
8384
$keys = $type->getKeyTypes();

src/Type/StaticTypeFactory.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type;
4+
5+
use PHPStan\Type\Constant\ConstantArrayType;
6+
use PHPStan\Type\Constant\ConstantBooleanType;
7+
use PHPStan\Type\Constant\ConstantFloatType;
8+
use PHPStan\Type\Constant\ConstantIntegerType;
9+
use PHPStan\Type\Constant\ConstantStringType;
10+
11+
class StaticTypeFactory
12+
{
13+
14+
public static function falsey(): Type
15+
{
16+
static $falsey;
17+
18+
if ($falsey === null) {
19+
$falsey = new UnionType([
20+
new NullType(),
21+
new ConstantBooleanType(false),
22+
new ConstantIntegerType(0),
23+
new ConstantFloatType(0.0),
24+
new ConstantStringType(''),
25+
new ConstantStringType('0'),
26+
new ConstantArrayType([], []),
27+
]);
28+
}
29+
30+
return $falsey;
31+
}
32+
33+
public static function truthy(): Type
34+
{
35+
static $truthy;
36+
37+
if ($truthy === null) {
38+
$truthy = new MixedType(false, self::falsey());
39+
}
40+
41+
return $truthy;
42+
}
43+
44+
}

src/Type/TypeUtils.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
use PHPStan\Type\Accessory\AccessoryType;
66
use PHPStan\Type\Accessory\HasPropertyType;
77
use PHPStan\Type\Constant\ConstantArrayType;
8-
use PHPStan\Type\Constant\ConstantBooleanType;
9-
use PHPStan\Type\Constant\ConstantFloatType;
10-
use PHPStan\Type\Constant\ConstantIntegerType;
118
use PHPStan\Type\Constant\ConstantStringType;
129

1310
class TypeUtils
@@ -342,34 +339,4 @@ public static function containsCallable(Type $type): bool
342339
return false;
343340
}
344341

345-
public static function falsey(): Type
346-
{
347-
static $falsey;
348-
349-
if ($falsey === null) {
350-
$falsey = new UnionType([
351-
new NullType(),
352-
new ConstantBooleanType(false),
353-
new ConstantIntegerType(0),
354-
new ConstantFloatType(0.0),
355-
new ConstantStringType(''),
356-
new ConstantStringType('0'),
357-
new ConstantArrayType([], []),
358-
]);
359-
}
360-
361-
return $falsey;
362-
}
363-
364-
public static function truthy(): Type
365-
{
366-
static $truthy;
367-
368-
if ($truthy === null) {
369-
$truthy = new MixedType(false, self::falsey());
370-
}
371-
372-
return $truthy;
373-
}
374-
375342
}

tests/PHPStan/Type/ArrayTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function dataIsSuperTypeOf(): array
5353
TrinaryLogic::createYes(),
5454
],
5555
[
56-
new ArrayType(new MixedType(), new MixedType(false, TypeUtils::falsey())),
56+
new ArrayType(new MixedType(), new MixedType(false, StaticTypeFactory::falsey())),
5757
new ConstantArrayType([], []),
5858
TrinaryLogic::createYes(),
5959
],

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,24 +1730,24 @@ public function dataUnion(): array
17301730
],
17311731
[
17321732
[
1733-
TypeUtils::falsey(),
1734-
TypeUtils::falsey(),
1733+
StaticTypeFactory::falsey(),
1734+
StaticTypeFactory::falsey(),
17351735
],
17361736
UnionType::class,
17371737
'0|0.0|\'\'|\'0\'|array()|false|null',
17381738
],
17391739
[
17401740
[
1741-
TypeUtils::truthy(),
1742-
TypeUtils::truthy(),
1741+
StaticTypeFactory::truthy(),
1742+
StaticTypeFactory::truthy(),
17431743
],
17441744
MixedType::class,
17451745
'mixed~0|0.0|\'\'|\'0\'|array()|false|null=implicit',
17461746
],
17471747
[
17481748
[
1749-
TypeUtils::falsey(),
1750-
TypeUtils::truthy(),
1749+
StaticTypeFactory::falsey(),
1750+
StaticTypeFactory::truthy(),
17511751
],
17521752
MixedType::class,
17531753
'mixed=implicit',
@@ -1932,24 +1932,24 @@ public function dataIntersect(): array
19321932
],
19331933
[
19341934
[
1935-
TypeUtils::truthy(),
1935+
StaticTypeFactory::truthy(),
19361936
new BooleanType(),
19371937
],
19381938
ConstantBooleanType::class,
19391939
'true',
19401940
],
19411941
[
19421942
[
1943-
TypeUtils::falsey(),
1943+
StaticTypeFactory::falsey(),
19441944
new BooleanType(),
19451945
],
19461946
ConstantBooleanType::class,
19471947
'false',
19481948
],
19491949
[
19501950
[
1951-
TypeUtils::falsey(),
1952-
TypeUtils::truthy(),
1951+
StaticTypeFactory::falsey(),
1952+
StaticTypeFactory::truthy(),
19531953
],
19541954
NeverType::class,
19551955
'*NEVER*',
@@ -3029,38 +3029,38 @@ public function dataRemove(): array
30293029
'*NEVER*',
30303030
],
30313031
[
3032-
TypeUtils::falsey(),
3033-
TypeUtils::falsey(),
3032+
StaticTypeFactory::falsey(),
3033+
StaticTypeFactory::falsey(),
30343034
NeverType::class,
30353035
'*NEVER*',
30363036
],
30373037
[
3038-
TypeUtils::truthy(),
3039-
TypeUtils::truthy(),
3038+
StaticTypeFactory::truthy(),
3039+
StaticTypeFactory::truthy(),
30403040
NeverType::class,
30413041
'*NEVER*',
30423042
],
30433043
[
3044-
TypeUtils::truthy(),
3045-
TypeUtils::falsey(),
3044+
StaticTypeFactory::truthy(),
3045+
StaticTypeFactory::falsey(),
30463046
MixedType::class,
30473047
'mixed~0|0.0|\'\'|\'0\'|array()|false|null',
30483048
],
30493049
[
3050-
TypeUtils::falsey(),
3051-
TypeUtils::truthy(),
3050+
StaticTypeFactory::falsey(),
3051+
StaticTypeFactory::truthy(),
30523052
UnionType::class,
30533053
'0|0.0|\'\'|\'0\'|array()|false|null',
30543054
],
30553055
[
30563056
new BooleanType(),
3057-
TypeUtils::falsey(),
3057+
StaticTypeFactory::falsey(),
30583058
ConstantBooleanType::class,
30593059
'true',
30603060
],
30613061
[
30623062
new BooleanType(),
3063-
TypeUtils::truthy(),
3063+
StaticTypeFactory::truthy(),
30643064
ConstantBooleanType::class,
30653065
'false',
30663066
],

0 commit comments

Comments
 (0)