Psalm: managing to confuse type resolver re: mixed > throw if not valid

Created on 11 Apr 2019  路  4Comments  路  Source: vimeo/psalm

<?php

/**
* @param mixed $width
* @param mixed $height
*
* @throws RuntimeException
*/
function Foo($width, $height) : void {
  if ( ! (is_int($width) || is_float($width)) || ! (is_int($height) || is_float($height))) {
    throw new RuntimeException('Width & Height were not numeric!');
  }

  echo sprintf('padding-top:%s%%;', 100 * ($height/$width));
}

Psalm output (using commit 34ebf5c):

INFO: MixedOperand - 14:44 - Left operand cannot be mixed

INFO: MixedArgument - 14:37 - Argument 2 of sprintf cannot be mixed, expecting string|int|float

The exception is thrown only if width or height are not either an int or a float, therefore in any subsequent code the types should be int|float

bug

All 4 comments

p.s. this issue also occurs with is_numeric.

is_numeric issue fixed in cadee4d

Improved handling slightly in 8751bf2 to work around a problem this fix introduced

Was this page helpful?
0 / 5 - 0 ratings

Related issues

caugner picture caugner  路  3Comments

greg0ire picture greg0ire  路  3Comments

vudaltsov picture vudaltsov  路  3Comments

albe picture albe  路  3Comments

Pierstoval picture Pierstoval  路  3Comments