Repository: spaze/phpstan-disallowed-calls (php)

Issue:
Union types are not supported in params
What the title says.

Pull request:
Support union types in params
The supported inner types is still only ConstantStringType.

Close #162

Patch:
diff --git a/src/DisallowedHelper.php b/src/DisallowedHelper.php
index c9ff56a..363e262 100644
--- a/src/DisallowedHelper.php
+++ b/src/DisallowedHelper.php
@@ -18,6 +18,7 @@ use PHPStan\Rules\RuleErrorBuilder;
 use PHPStan\Type\ObjectType;
 use PHPStan\Type\Type;
 use PHPStan\Type\TypeWithClassName;
+use PHPStan\Type\UnionType;
 use Spaze\PHPStan\Rules\Disallowed\Params\DisallowedCallParam;
 
 class DisallowedHelper
@@ -111,8 +112,15 @@ class DisallowedHelper
 			if ($type === null) {
 				return !$paramsRequired;
 			}
-			if (!$param->matches($type)) {
-				return false;
+			if ($type instanceof UnionType) {
+				$types = $type->getTypes();
+			} else {
+				$types = [$type];
+			}
+			foreach ($types as $type) {
+				if (!$param->matches($type)) {
+					return false;
+				}
 			}
 		}
 		return true;


Test patch:
diff --git a/tests/Calls/FunctionCallsTest.php b/tests/Calls/FunctionCallsTest.php
index d5d4808..cf080bd 100644
--- a/tests/Calls/FunctionCallsTest.php
+++ b/tests/Calls/FunctionCallsTest.php
@@ -249,39 +249,39 @@ class FunctionCallsTest extends RuleTestCase
 			],
 			[
 				'Calling setcookie() is forbidden, because reasons',
-				57,
+				59,
 			],
 			[
 				'Calling header() is forbidden, because reasons',
-				62,
+				64,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				67,
+				69,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				68,
+				70,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				69,
+				71,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				70,
+				72,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				71,
+				73,
 			],
 			[
 				'Calling array_filter() is forbidden, callback parameter must be given.',
-				74,
+				76,
 			],
 			[
 				'Calling mocky() is forbidden, mocking Blade is not allowed.',
-				81,
+				83,
 			],
 		]);
 		// Based on the configuration above, no errors in this file:
@@ -292,27 +292,27 @@ class FunctionCallsTest extends RuleTestCase
 			],
 			[
 				'Calling setcookie() is forbidden, because reasons',
-				57,
+				59,
 			],
 			[
 				'Calling setcookie() is forbidden, because reasons',
-				58,
+				60,
 			],
 			[
 				'Calling header() is forbidden, because reasons',
-				62,
+				64,
 			],
 			[
 				'Calling header() is forbidden, because reasons',
-				63,
+				65,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				67,
+				69,
 			],
 			[
 				'Calling htmlspecialchars() is forbidden, because reasons',
-				68,
+				70,
 			],
 		]);
 	}
diff --git a/tests/src/disallowed-allow/functionCalls.php b/tests/src/disallowed-allow/functionCalls.php
index b60c7f8..569b3b4 100644
--- a/tests/src/disallowed-allow/functionCalls.php
+++ b/tests/src/disallowed-allow/functionCalls.php
@@ -51,7 +51,9 @@ hash('md5', 'nope');
 hash('Md5', 'nOpE');
 hash('sha1', 'nah');
 hash('SHA1', 'NAH');
-hash('sha256', 'oh yeah but not for passwords tho');
+/** @var 'sha256'|'sha384'|'sha512' $okay */
+$okay = 'sha256';
+hash($okay, 'oh yeah but not for passwords tho');
 
 // third param needed
 setcookie('foo', 'bar');
diff --git a/tests/src/disallowed/functionCalls.php b/tests/src/disallowed/functionCalls.php
index e71a0f5..0f0299a 100644
--- a/tests/src/disallowed/functionCalls.php
+++ b/tests/src/disallowed/functionCalls.php
@@ -51,7 +51,9 @@ hash('md5', 'nope');
 hash('Md5', 'nOpE');
 hash('sha1', 'nah');
 hash('SHA1', 'NAH');
-hash('sha256', 'oh yeah but not for passwords tho');
+/** @var 'sha256'|'sha384'|'sha512' $okay */
+$okay = 'sha256';
+hash($okay, 'oh yeah but not for passwords tho');
 
 // second param needed
 setcookie('foo', 'bar');