| Total Complexity | 5 |
| Total Lines | 27 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | final class WidthMeasurer implements IWidthMeasurer |
||
| 14 | { |
||
| 15 | public function __construct( |
||
| 16 | protected Closure $measureFunction, |
||
| 17 | ) { |
||
| 18 | self::assert($this->measureFunction); |
||
| 19 | } |
||
| 20 | |||
| 21 | private static function assert(Closure $measureFunction): void |
||
| 22 | { |
||
| 23 | $reflection = new ReflectionFunction($measureFunction); |
||
| 24 | $returnType = $reflection->getReturnType()?->getName(); |
||
|
|
|||
| 25 | $parameterType = $reflection->getParameters()[0]->getType()?->getName(); |
||
| 26 | |||
| 27 | if ($parameterType === 'string' && $returnType === 'int') { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | throw new InvalidArgumentException( |
||
| 32 | 'Invalid measure function signature.' |
||
| 33 | . ' Signature expected to be: "function(string $string): int { //... }".' |
||
| 34 | ); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function measureWidth(string $string): int |
||
| 40 | } |
||
| 41 | } |
||
| 42 |