Repository: phpstan/phpstan-doctrine (php)

Issue:
`CASE WHEN EXISTS(...) THEN TRUE ELSE FALSE END AS my_column` is not inferred as boolean
- Create an ORM QueryBuilder
- Call `->select('CASE WHEN EXISTS(SELECT 1) THEN TRUE ELSE FALSE END AS my_column')`
- Call `->getQuery()->getResult()`

See the type of `my_column` inferred as `0|'0'`

Pull request description:
Fixing handling of boolean literals
Fixes #311

Patch:
diff --git a/src/Type/Doctrine/Query/QueryResultTypeWalker.php b/src/Type/Doctrine/Query/QueryResultTypeWalker.php
index 66158af..18a6d41 100644
--- a/src/Type/Doctrine/Query/QueryResultTypeWalker.php
+++ b/src/Type/Doctrine/Query/QueryResultTypeWalker.php
@@ -44,6 +44,7 @@ use function is_object;
 use function is_string;
 use function serialize;
 use function sprintf;
+use function strtolower;
 use function unserialize;
 
 /**
@@ -1095,7 +1096,7 @@ class QueryResultTypeWalker extends SqlWalker
 				break;
 
 			case AST\Literal::BOOLEAN:
-				$value = $literal->value === 'true' ? 1 : 0;
+				$value = strtolower($literal->value) === 'true' ? 1 : 0;
 				$type = new ConstantIntegerType($value);
 				break;
 


Test patch:
diff --git a/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php b/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
index cc03fa5..87b66d0 100644
--- a/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
+++ b/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
@@ -667,6 +667,82 @@ final class QueryResultTypeWalkerTest extends PHPStanTestCase
 					FROM		QueryResult\Entities\Many m
 				',
 			],
+			'Issue 311' => [
+				$this->constantArray([
+					[
+						new ConstantIntegerType(1),
+						TypeCombinator::union(
+							new ConstantIntegerType(0),
+							new ConstantIntegerType(1),
+							new ConstantStringType('0'),
+							new ConstantStringType('1')
+						),
+					],
+				]),
+				'
+					SELECT		CASE
+									WHEN m.intColumn < 10 THEN true
+									ELSE false
+								END
+					FROM		QueryResult\Entities\Many m
+				',
+			],
+			'Issue 311 - 2' => [
+				$this->constantArray([
+					[
+						new ConstantIntegerType(1),
+						TypeCombinator::union(
+							new ConstantIntegerType(0),
+							new ConstantIntegerType(1),
+							new ConstantStringType('0'),
+							new ConstantStringType('1')
+						),
+					],
+				]),
+				'
+					SELECT		CASE
+									WHEN m.intColumn < 10 THEN TRUE
+									ELSE FALSE
+								END
+					FROM		QueryResult\Entities\Many m
+				',
+			],
+			'Issue 311 - 3' => [
+				$this->constantArray([
+					[
+						new ConstantIntegerType(1),
+						TypeCombinator::union(
+							new ConstantIntegerType(1),
+							new ConstantStringType('1')
+						),
+					],
+					[
+						new ConstantIntegerType(2),
+						TypeCombinator::union(
+							new ConstantIntegerType(0),
+							new ConstantStringType('0')
+						),
+					],
+					[
+						new ConstantIntegerType(3),
+						TypeCombinator::union(
+							new ConstantIntegerType(1),
+							new ConstantStringType('1')
+						),
+					],
+					[
+						new ConstantIntegerType(4),
+						TypeCombinator::union(
+							new ConstantIntegerType(0),
+							new ConstantStringType('0')
+						),
+					],
+				]),
+				'
+					SELECT		(TRUE), (FALSE), (true), (false)
+					FROM		QueryResult\Entities\Many m
+				',
+			],
 			'new' => [
 				new ObjectType(ManyId::class),
 				'