@@ -115,22 +115,46 @@ private function compareTypes(Call $call, array $casts, Constructor $constructor
115115 */
116116 private function checkType (Call $ call , string $ key , UnionType $ actualType , array $ casts , UnionType $ expectedType ): ?RuleError
117117 {
118- // Ignore cases, where there exists a cast - since we cannot analyse them in dept
118+ // Casters cannot cast nullable values, so quick test for type error
119+ // is simply to check if the expected type accepts null values or not.
120+ if ($ actualType ->isNullable () && $ expectedType ->isNotNullable ()) {
121+ return $ this ->buildError ($ call , $ key , $ expectedType , $ actualType );
122+ }
123+
124+ // Otherwise, ignore cases where there exists a cast - since we cannot analyse them in dept.
119125 if ($ this ->expectedTypesMatchesExactlyCast ($ casts , $ expectedType )) {
120126 return null ;
121127 }
122128
129+ // Run full type inspection and return any errors found.
123130 if ( ! TypeSystem::isSubtypeOf ($ actualType , $ expectedType )) {
124- return RuleErrorBuilder::message (self ::getErrorMessage ($ key , $ call ->target , $ expectedType , $ actualType ))
125- ->line ($ call ->method ->line )
126- ->file ($ call ->method ->file )
127- ->tip ('This is a custom CEGO rule, if you found a bug fix it in the cego/phpstan project ' )
128- ->build ();
131+ return $ this ->buildError ($ call , $ key , $ expectedType , $ actualType );
129132 }
130133
131134 return null ;
132135 }
133136
137+ /**
138+ * Builds a RuleError instance
139+ *
140+ * @param Call $call
141+ * @param string $key
142+ * @param string $expectedType
143+ * @param string $actualType
144+ *
145+ * @throws ShouldNotHappenException
146+ *
147+ * @return RuleError
148+ */
149+ private function buildError (Call $ call , string $ key , string $ expectedType , string $ actualType ): RuleError
150+ {
151+ return RuleErrorBuilder::message (self ::getErrorMessage ($ key , $ call ->target , $ expectedType , $ actualType ))
152+ ->line ($ call ->method ->line )
153+ ->file ($ call ->method ->file )
154+ ->tip ('This is a custom CEGO rule, if you found a bug fix it in the cego/phpstan project ' )
155+ ->build ();
156+ }
157+
134158 /**
135159 * Returns the error message to give the developer on errors
136160 *
0 commit comments