| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Assert; |
| 4 |
|
| 5 |
/** |
| 6 |
* Assert provides functions for assorting preconditions (such as parameter types) and |
| 7 |
* postconditions. It is intended as a safer alternative to PHP's assert() function. |
| 8 |
* |
| 9 |
* Note that assertions evaluate expressions and add function calls, so using assertions |
| 10 |
* may have a negative impact on performance when used in performance hotspots. The idea |
| 11 |
* if this class is to have a neat tool for assertions if and when they are needed. |
| 12 |
* It is not recommended to place assertions all over the code indiscriminately. |
| 13 |
* |
| 14 |
* For more information, see the the README file. |
| 15 |
* |
| 16 |
* @since 0.1.0 |
| 17 |
* |
| 18 |
* @license MIT |
| 19 |
* @author Daniel Kinzler |
| 20 |
* @author Thiemo Kreuz |
| 21 |
* @copyright Wikimedia Deutschland e.V. |
| 22 |
*/ |
| 23 |
class Assert { |
| 24 |
|
| 25 |
/** |
| 26 |
* Checks a precondition, that is, throws a PreconditionException if $condition is false. |
| 27 |
* For checking call parameters, use Assert::parameter() instead. |
| 28 |
* |
| 29 |
* This is provided for completeness, most preconditions should be covered by |
| 30 |
* Assert::parameter() and related assertions. |
| 31 |
* |
| 32 |
* @see parameter() |
| 33 |
* |
| 34 |
* @note This is intended mostly for checking preconditions in constructors and setters, |
| 35 |
* or before using parameters in complex computations. |
| 36 |
* Checking preconditions in every function call is not recommended, since it may have a |
| 37 |
* negative impact on performance. |
| 38 |
* |
| 39 |
* @since 0.1.0 |
| 40 |
* |
| 41 |
* @param bool $condition |
| 42 |
* @param string $description The message to include in the exception if the condition fails. |
| 43 |
* |
| 44 |
* @throws PreconditionException if $condition is not true. |
| 45 |
* @phan-assert-true-condition $condition |
| 46 |
*/ |
| 47 |
public static function precondition( $condition, $description ): void { |
| 48 |
if ( !$condition ) { |
| 49 |
throw new PreconditionException( "Precondition failed: $description" ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Checks a parameter, that is, throws a ParameterAssertionException if $condition is false. |
| 55 |
* This is similar to Assert::precondition(). |
| 56 |
* |
| 57 |
* @note This is intended for checking parameters in constructors and setters. |
| 58 |
* Checking parameters in every function call is not recommended, since it may have a |
| 59 |
* negative impact on performance. |
| 60 |
* |
| 61 |
* @since 0.1.0 |
| 62 |
* |
| 63 |
* @param bool $condition |
| 64 |
* @param string $name The name of the parameter that was checked. |
| 65 |
* @param string $description The message to include in the exception if the condition fails. |
| 66 |
* |
| 67 |
* @throws ParameterAssertionException if $condition is not true. |
| 68 |
* @phan-assert-true-condition $condition |
| 69 |
*/ |
| 70 |
public static function parameter( $condition, $name, $description ): void { |
| 71 |
if ( !$condition ) { |
| 72 |
throw new ParameterAssertionException( $name, $description ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Checks an parameter's type, that is, throws a InvalidArgumentException if $value is |
| 78 |
* not of $type. This is really a special case of Assert::precondition(). |
| 79 |
* |
| 80 |
* @note This is intended for checking parameters in constructors and setters. |
| 81 |
* Checking parameters in every function call is not recommended, since it may have a |
| 82 |
* negative impact on performance. |
| 83 |
* |
| 84 |
* @note If possible, type hints should be used instead of calling this function. |
| 85 |
* It is intended for cases where type hints to not work, e.g. for checking union types. |
| 86 |
* |
| 87 |
* @since 0.1.0 |
| 88 |
* |
| 89 |
* @param string|string[] $types The parameter's expected type. Can be the name of a native type |
| 90 |
* or a class or interface, or a list of such names. |
| 91 |
* For compatibility with versions before 0.4.0, multiple types can also be given separated |
| 92 |
* by pipe characters ("|"). |
| 93 |
* @param mixed $value The parameter's actual value. |
| 94 |
* @param string $name The name of the parameter that was checked. |
| 95 |
* |
| 96 |
* @throws ParameterTypeException if $value is not of type (or, for objects, is not an |
| 97 |
* instance of) $type. |
| 98 |
* |
| 99 |
*/ |
| 100 |
public static function parameterType( $types, $value, $name ): void { |
| 101 |
if ( is_string( $types ) ) { |
| 102 |
$types = explode( '|', $types ); |
| 103 |
} |
| 104 |
if ( !self::hasType( $value, $types ) ) { |
| 105 |
throw new ParameterTypeException( $name, implode( '|', $types ) ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @since 0.3.0 |
| 111 |
* |
| 112 |
* @param string $type Either "integer" or "string". Mixing "integer|string" is not supported |
| 113 |
* because this is PHP's default anyway. It is of no value to check this. |
| 114 |
* @param array $value The parameter's actual value. If this is not an array, a |
| 115 |
* ParameterTypeException is raised. |
| 116 |
* @param string $name The name of the parameter that was checked. |
| 117 |
* |
| 118 |
* @throws ParameterTypeException if one of the keys in the array $value is not of type $type. |
| 119 |
*/ |
| 120 |
public static function parameterKeyType( $type, $value, $name ): void { |
| 121 |
self::parameterType( 'array', $value, $name ); |
| 122 |
|
| 123 |
if ( $type !== 'integer' && $type !== 'string' ) { |
| 124 |
throw new ParameterAssertionException( 'type', 'must be "integer" or "string"' ); |
| 125 |
} |
| 126 |
|
| 127 |
foreach ( $value as $key => $element ) { |
| 128 |
if ( gettype( $key ) !== $type ) { |
| 129 |
throw new ParameterKeyTypeException( $name, $type ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Checks the type of all elements of an parameter, assuming the parameter is an array, |
| 136 |
* that is, throws a ParameterElementTypeException if any elements in $value are not of $type. |
| 137 |
* |
| 138 |
* @note This is intended for checking parameters in constructors and setters. |
| 139 |
* Checking parameters in every function call is not recommended, since it may have a |
| 140 |
* negative impact on performance. |
| 141 |
* |
| 142 |
* @since 0.1.0 |
| 143 |
* |
| 144 |
* @param string|string[] $types The elements' expected type. Can be the name of a native type |
| 145 |
* or a class or interface. Multiple types can be given in an array (or a string separated |
| 146 |
* by a pipe character ("|"), for compatibility with versions before 0.5.0). |
| 147 |
* @param array $value The parameter's actual value. If this is not an array, |
| 148 |
* a ParameterTypeException is raised. |
| 149 |
* @param string $name The name of the parameter that was checked. |
| 150 |
* |
| 151 |
* @throws ParameterTypeException If $value is not an array. |
| 152 |
* @throws ParameterElementTypeException If an element of $value is not of type |
| 153 |
* (or, for objects, is not an instance of) $type. |
| 154 |
* |
| 155 |
*/ |
| 156 |
public static function parameterElementType( $types, $value, $name ): void { |
| 157 |
self::parameterType( 'array', $value, $name ); |
| 158 |
if ( is_string( $types ) ) { |
| 159 |
$types = explode( '|', $types ); |
| 160 |
} |
| 161 |
|
| 162 |
foreach ( $value as $element ) { |
| 163 |
if ( !self::hasType( $element, $types ) ) { |
| 164 |
throw new ParameterElementTypeException( $name, implode( '|', $types ) ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @since 0.3.0 |
| 171 |
* |
| 172 |
* @param string $value |
| 173 |
* @param string $name |
| 174 |
* |
| 175 |
* @throws ParameterTypeException if $value is not a non-empty string. |
| 176 |
* @phan-assert non-empty-string $value |
| 177 |
*/ |
| 178 |
public static function nonEmptyString( $value, $name ): void { |
| 179 |
if ( !is_string( $value ) || $value === '' ) { |
| 180 |
throw new ParameterTypeException( $name, 'non-empty string' ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Checks a postcondition, that is, throws a PostconditionException if $condition is false. |
| 186 |
* This is very similar Assert::invariant() but is intended for use only after a computation |
| 187 |
* is complete. |
| 188 |
* |
| 189 |
* @note This is intended for double checking in the implementation of complex algorithms. |
| 190 |
* Note however that it should not be used in performance hotspots, since evaluating |
| 191 |
* $condition and calling postcondition() costs time. |
| 192 |
* |
| 193 |
* @since 0.1.0 |
| 194 |
* |
| 195 |
* @param bool $condition |
| 196 |
* @param string $description The message to include in the exception if the condition fails. |
| 197 |
* |
| 198 |
* @throws PostconditionException |
| 199 |
* @phan-assert-true-condition $condition |
| 200 |
*/ |
| 201 |
public static function postcondition( $condition, $description ): void { |
| 202 |
if ( !$condition ) { |
| 203 |
throw new PostconditionException( "Postcondition failed: $description" ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Checks an invariant, that is, throws a InvariantException if $condition is false. |
| 209 |
* This is very similar Assert::postcondition() but is intended for use throughout the code. |
| 210 |
* |
| 211 |
* @note The $condition is expected to be falsifiable. If you are trying |
| 212 |
* to indicate that a code path is unreachable, use |
| 213 |
* `throw new UnreachableException( 'why this code is unreachable' )` |
| 214 |
* instead of `Assert::invariant( false, '…' )`. Code checking tools |
| 215 |
* will complain about the latter. |
| 216 |
* |
| 217 |
* @note This is intended for double checking in the implementation of complex algorithms. |
| 218 |
* Note however that it should not be used in performance hotspots, since evaluating |
| 219 |
* $condition and calling invariant() costs time. |
| 220 |
* |
| 221 |
* @since 0.1.0 |
| 222 |
* |
| 223 |
* @param bool $condition |
| 224 |
* @param string $description The message to include in the exception if the condition fails. |
| 225 |
* |
| 226 |
* @throws InvariantException |
| 227 |
* @phan-assert-true-condition $condition |
| 228 |
*/ |
| 229 |
public static function invariant( $condition, $description ): void { |
| 230 |
if ( !$condition ) { |
| 231 |
throw new InvariantException( "Invariant failed: $description" ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @param mixed $value |
| 237 |
* @param string[] $allowedTypes |
| 238 |
* |
| 239 |
* @return bool |
| 240 |
*/ |
| 241 |
private static function hasType( $value, array $allowedTypes ): bool { |
| 242 |
// Apply strtolower because gettype returns "NULL" for null values. |
| 243 |
$type = strtolower( gettype( $value ) ); |
| 244 |
|
| 245 |
if ( in_array( $type, $allowedTypes ) ) { |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
if ( in_array( 'callable', $allowedTypes ) && is_callable( $value ) ) { |
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
if ( is_object( $value ) && self::isInstanceOf( $value, $allowedTypes ) ) { |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
if ( is_array( $value ) && in_array( 'Traversable', $allowedTypes ) ) { |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
if ( $value === false && in_array( 'false', $allowedTypes ) ) { |
| 262 |
return true; |
| 263 |
} |
| 264 |
if ( $value === true && in_array( 'true', $allowedTypes ) ) { |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* @param object $value |
| 273 |
* @param string[] $allowedTypes |
| 274 |
* |
| 275 |
* @return bool |
| 276 |
*/ |
| 277 |
private static function isInstanceOf( $value, array $allowedTypes ): bool { |
| 278 |
foreach ( $allowedTypes as $type ) { |
| 279 |
if ( $value instanceof $type ) { |
| 280 |
return true; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
} |