$element ) { if ( gettype( $key ) !== $type ) { throw new ParameterKeyTypeException( $name, $type ); } } } /** * Checks the type of all elements of an parameter, assuming the parameter is an array, * that is, throws a ParameterElementTypeException if any elements in $value are not of $type. * * @note This is intended for checking parameters in constructors and setters. * Checking parameters in every function call is not recommended, since it may have a * negative impact on performance. * * @since 0.1.0 * * @param string|string[] $types The elements' expected type. Can be the name of a native type * or a class or interface. Multiple types can be given in an array (or a string separated * by a pipe character ("|"), for compatibility with versions before 5.0). * @param array $value The parameter's actual value. If this is not an array, * a ParameterTypeException is raised. * @param string $name The name of the parameter that was checked. * * @throws ParameterTypeException If $value is not an array. * @throws ParameterElementTypeException If an element of $value is not of type * (or, for objects, is not an instance of) $type. * */ public static function parameterElementType( $types, $value, $name ) { self::parameterType( 'array', $value, $name ); if ( is_string( $types ) ) { $types = explode( '|', $types ); } foreach ( $value as $element ) { if ( !self::hasType( $element, $types ) ) { throw new ParameterElementTypeException( $name, implode( '|', $types ) ); } } } /** * @since 0.3.0 * * @param string $value * @param string $name * * @throws ParameterTypeException if $value is not a non-empty string. */ public static function nonEmptyString( $value, $name ) { if ( !is_string( $value ) || $value === '' ) { throw new ParameterTypeException( $name, 'non-empty string' ); } } /** * Checks a postcondition, that is, throws a PostconditionException if $condition is false. * This is very similar Assert::invariant() but is intended for use only after a computation * is complete. * * @note This is intended for sanity-checks in the implementation of complex algorithms. * Note however that it should not be used in performance hotspots, since evaluating * $condition and calling postcondition() costs time. * * @since 0.1.0 * * @param bool $condition * @param string $description The message to include in the exception if the condition fails. * * @throws PostconditionException * @phan-assert-true-condition $condition */ public static function postcondition( $condition, $description ) { if ( !$condition ) { throw new PostconditionException( "Postcondition failed: $description" ); } } /** * Checks an invariant, that is, throws a InvariantException if $condition is false. * This is very similar Assert::postcondition() but is intended for use throughout the code. * * @note This is intended for sanity-checks in the implementation of complex algorithms. * Note however that it should not be used in performance hotspots, since evaluating * $condition and calling invariant() costs time. * * @since 0.1.0 * * @param bool $condition * @param string $description The message to include in the exception if the condition fails. * * @throws InvariantException * @phan-assert-true-condition $condition */ public static function invariant( $condition, $description ) { if ( !$condition ) { throw new InvariantException( "Invariant failed: $description" ); } } /** * @param mixed $value * @param string[] $allowedTypes * * @return bool */ private static function hasType( $value, array $allowedTypes ) { // Apply strtolower because gettype returns "NULL" for null values. $type = strtolower( gettype( $value ) ); if ( in_array( $type, $allowedTypes ) ) { return true; } if ( in_array( 'callable', $allowedTypes ) && is_callable( $value ) ) { return true; } if ( is_object( $value ) && self::isInstanceOf( $value, $allowedTypes ) ) { return true; } if ( is_array( $value ) && in_array( 'Traversable', $allowedTypes ) ) { return true; } return false; } /** * @param object $value * @param string[] $allowedTypes * * @return bool */ private static function isInstanceOf( $value, array $allowedTypes ) { foreach ( $allowedTypes as $type ) { if ( $value instanceof $type ) { return true; } } return false; } }