Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReflectionMethodToReflectionClassConverter
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
9.86
0.00% covered (danger)
0.00%
0 / 1
 convert
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
9.86
1<?php
2namespace Apie\Core\TypeConverters;
3
4use Apie\TypeConverter\ConverterInterface;
5use Apie\TypeConverter\Exceptions\CanNotConvertObjectException;
6use Apie\TypeConverter\ReflectionTypeFactory;
7use Apie\TypeConverter\TypeConverter;
8use ReflectionClass;
9use ReflectionMethod;
10use ReflectionType;
11
12/**
13 * @implements ConverterInterface<ReflectionMethod, ReflectionClass>
14 */
15class ReflectionMethodToReflectionClassConverter implements ConverterInterface
16{
17    /**
18     * @return ReflectionClass<object>|null
19     */
20    public function convert(ReflectionMethod $input, ?ReflectionType $wantedType = null, ?TypeConverter $converter = null): ?ReflectionClass
21    {
22        $wantedType ??= ReflectionTypeFactory::createReflectionType(ReflectionClass::class);
23        if (str_starts_with($input->getName(), 'set') && $input->getNumberOfRequiredParameters() > 0) {
24            $arguments = $input->getParameters();
25            return $converter->convertTo(reset($arguments), $wantedType);
26        }
27        foreach (['has', 'is', 'get'] as $methodPrefix) {
28            if (str_starts_with($input->getName(), $methodPrefix)) {
29                return $converter->convertTo($input->getReturnType(), $wantedType);
30            }
31        }
32        foreach (['toNative', 'offsetGet'] as $methodName) {
33            if ($input->getName() === $methodName) {
34                return $converter->convertTo($input->getReturnType(), $wantedType);
35            }
36        }
37
38        if (!$wantedType->allowsNull()) {
39            throw new CanNotConvertObjectException($input, $wantedType);
40        }
41
42        return null;
43    }
44}