Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Analyzer | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
| getType | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
8 | |||
| isScalar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| typeMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Analyzer class implementation file |
| 4 | * @package Code\Type |
| 5 | * @author Felipe Gaspar <felipesouzalimagaspar@gmail.com> |
| 6 | */ |
| 7 | declare(strict_types=1); |
| 8 | namespace Code\Type; |
| 9 | /** |
| 10 | * Analyzer is an inspection class for primitive types and classes in PHP |
| 11 | * @final |
| 12 | */ |
| 13 | final class Analyzer { |
| 14 | /** |
| 15 | * Checks the type or class name of a variable |
| 16 | * @access public |
| 17 | * @param mixed $param Variable you want to find out the type |
| 18 | * @param bool $returnClassName Flag to enable/disable inspection of class names |
| 19 | * @return string Type or class name of the informed variable |
| 20 | */ |
| 21 | public static function getType(mixed $param, bool $returnClassName = true) : string { |
| 22 | if($param instanceof \ReflectionNamedType) { |
| 23 | return $param->getName(); |
| 24 | } else if($returnClassName && gettype($param) === 'object') { |
| 25 | return get_class($param); |
| 26 | }else { |
| 27 | $format = fn (string $t) => $t === 'boolean' ? 'bool' : ($t === 'integer' ? 'int' : ($t === 'double' ? 'float' : ($t === 'NULL' ? 'null' : $t))); |
| 28 | return $format(gettype($param)); |
| 29 | } |
| 30 | } |
| 31 | /** |
| 32 | * Check if a variable is scalar (bool, int, float, string, or null) |
| 33 | * @access public |
| 34 | * @param mixed $param Variable you want to find out the type |
| 35 | * @return bool The variable is or is not scalar |
| 36 | */ |
| 37 | public static function isScalar(mixed $param) : bool { return in_array(self::getType($param), ['bool', 'int', 'float', 'string', 'null']); } |
| 38 | /** |
| 39 | * Checks if a variable is an array |
| 40 | * @access public |
| 41 | * @param mixed $param Variable you want to find out the type |
| 42 | * @return bool The variable is or is not array |
| 43 | */ |
| 44 | public static function isArray(mixed $param) : bool { return self::getType($param) === 'array'; } |
| 45 | /** |
| 46 | * Checks if a variable is an object |
| 47 | * @access public |
| 48 | * @param mixed $param Variable you want to find out the type |
| 49 | * @return bool The variable is or is not object |
| 50 | */ |
| 51 | public static function isObject(mixed $param) : bool { return self::getType($param, false) === 'object'; } |
| 52 | /** |
| 53 | * Checks if two variables are of the same type |
| 54 | * @access public |
| 55 | * @param mixed $param1 Variable 1 you want to compare type |
| 56 | * @param mixed $param2 Variable 2 you want to compare type |
| 57 | * @return bool The variables are or are not of the same type |
| 58 | */ |
| 59 | public static function typeMatch(mixed $param1, mixed $param2) : bool { return self::getType($param1) === self::getType($param2); } |
| 60 | } |