Issues (10)

src/TypeUtils.php (1 issue)

1
<?php
2
3
4
namespace Apie\ObjectAccessNormalizer;
5
6
use Apie\ObjectAccessNormalizer\Getters\GetterInterface;
7
use Apie\ObjectAccessNormalizer\Setters\SetterInterface;
8
use ReflectionMethod;
9
use ReflectionProperty;
10
use Symfony\Component\PropertyInfo\Type;
11
12
class TypeUtils
13
{
14
    /**
15
     * @param GetterInterface[]|SetterInterface[] $methods
16
     * @return Type[]
17
     */
18
    public static function convertToTypeArray(array $methods)
19
    {
20
        $res = [];
21
        foreach ($methods as $method) {
22
            $type = $method->toType();
23
            if ($type) {
24
                $res[] = $type;
25
            }
26
        }
27
        return $res;
28
    }
29
30
    /**
31
     * Check if the method is a getter or setter and returns a typehint if available.
32
     *
33
     * @param ReflectionMethod $property
34
     * @return Type|null
35
     */
36
    public static function convertMethodToType(ReflectionMethod $method): ?Type
37
    {
38
        $parameters = $method->getParameters();
39
        $parameter = reset($parameters);
40
        $type = null;
41
        if ($parameter && !$parameter->isOptional()) {
42
            $type = $parameter->getType();
43
        } elseif (!$parameter) {
44
            $type = $method->getReturnType();
45
        }
46
        if (!$type) {
47
            return null;
48
        }
49
        if ($type->isBuiltin()) {
50
            return new Type($type->getName(), $type->allowsNull());
51
        }
52
        return new Type(Type::BUILTIN_TYPE_OBJECT, $type->allowsNull(), $type->getName());
53
    }
54
55
    /**
56
     * If PHP version is higher than 7.4 return typehint of property.
57
     *
58
     * @param ReflectionProperty $property
59
     * @return Type|null
60
     */
61
    public static function convertPropertyToType(ReflectionProperty $property): ?Type
62
    {
63
        if (PHP_VERSION_ID >= 70400) {
64
            $type = $property->getType();
65
            if (!$type) {
66
                return null;
67
            }
68
            if ($type->isBuiltin()) {
69
                return new Type($type->getName(), $type->allowsNull());
0 ignored issues
show
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
                return new Type($type->/** @scrutinizer ignore-call */ getName(), $type->allowsNull());
Loading history...
70
            }
71
            return new Type(Type::BUILTIN_TYPE_OBJECT, $type->allowsNull(), $type->getName());
72
        }
73
        return null;
74
    }
75
76
    /**
77
     * Checks if a method's name is get.* or is.*, and can be called without parameters.
78
     */
79
    public static function isGetMethod(ReflectionMethod $method): bool
80
    {
81
        return
82
            !$method->isStatic()
83
            && preg_match('/^(get|is|has)[A-Z0-9]/i', $method->name)
84
            && 0 === $method->getNumberOfRequiredParameters();
85
    }
86
87
    /**
88
     * Checks if a method's name is set.*  with 0 or 1 parameters.
89
     */
90
    public static function isSetMethod(ReflectionMethod $method): bool
91
    {
92
        return
93
            !$method->isStatic()
94
            && preg_match('/^set[A-Z0-9]/i', $method->name)
95
            && 2 > $method->getNumberOfRequiredParameters();
96
    }
97
}
98