Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 Copyright (c) 2009 hamcrest.org
5
 */
6
 
7
class FactoryParameter
8
{
9
    /**
10
     * @var FactoryMethod
11
     */
12
    private $method;
13
 
14
    /**
15
     * @var ReflectionParameter
16
     */
17
    private $reflector;
18
 
19
    public function __construct(FactoryMethod $method, ReflectionParameter $reflector)
20
    {
21
        $this->method = $method;
22
        $this->reflector = $reflector;
23
    }
24
 
25
    /**
26
     * Compute the declaration code.
27
     *
28
     * @return string
29
     */
30
    public function getDeclaration()
31
    {
32
        $code = $this->getTypeCode() . $this->getInvocation();
33
 
34
        if ($this->reflector->isOptional()) {
35
            $default = $this->reflector->getDefaultValue();
36
            if (is_null($default)) {
37
                $default = 'null';
38
            } elseif (is_bool($default)) {
39
                $default = $default ? 'true' : 'false';
40
            } elseif (is_string($default)) {
41
                $default = "'" . $default . "'";
42
            } elseif (is_numeric($default)) {
43
                $default = strval($default);
44
            } elseif (is_array($default)) {
45
                $default = 'array()';
46
            } else {
47
                echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . "\n";
48
                var_dump($default);
49
                $default = 'null';
50
            }
51
            $code .= ' = ' . $default;
52
        }
53
        return $code;
54
    }
55
 
56
    /**
57
     * Compute the type code for the paramater.
58
     *
59
     * @return string
60
     */
61
    private function getTypeCode()
62
    {
63
        // Handle PHP 5 separately
64
        if (PHP_VERSION_ID < 70000) {
65
            if ($this->reflector->isArray()) {
66
                return 'array';
67
            }
68
 
69
            $class = $this->reflector->getClass();
70
 
71
            return $class ? sprintf('\\%s ', $class->getName()) : '';
72
        }
73
 
74
        if (!$this->reflector->hasType()) {
75
            return '';
76
        }
77
 
78
        $type = $this->reflector->getType();
79
        $name = self::getQualifiedName($type);
80
 
81
        // PHP 7.1+ supports nullable types via a leading question mark
82
        return (PHP_VERSION_ID >= 70100 && $type->allowsNull()) ? sprintf('?%s ', $name) : sprintf('%s ', $name);
83
    }
84
 
85
    /**
86
     * Compute qualified name for the given type.
87
     *
88
     * This function knows how to prefix class names with a leading slash and
89
     * also how to handle PHP 8's union types.
90
     *
91
     * @param ReflectionType $type
92
     *
93
     * @return string
94
     */
95
    private static function getQualifiedName(ReflectionType $type)
96
    {
97
        // PHP 8 union types can be recursively processed
98
        if ($type instanceof ReflectionUnionType) {
99
            return implode('|', array_map(function (ReflectionType $type) {
100
                // The "self::" call within a Closure is fine here because this
101
                // code will only ever be executed on PHP 7.0+
102
                return self::getQualifiedName($type);
103
            }, $type->getTypes()));
104
        }
105
 
106
        // PHP 7.0 doesn't have named types, but 7.1+ does
107
        $name = $type instanceof ReflectionNamedType ? $type->getName() : (string) $type;
108
 
109
        return $type->isBuiltin() ? $name : sprintf('\\%s', $name);
110
    }
111
 
112
    /**
113
     * Compute the invocation code.
114
     *
115
     * @return string
116
     */
117
    public function getInvocation()
118
    {
119
        return sprintf('$%s', $this->reflector->getName());
120
    }
121
 
122
    /**
123
     * Compute the method name.
124
     *
125
     * @return string
126
     */
127
    public function getMethod()
128
    {
129
        return $this->method;
130
    }
131
}