Parameter::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Mockery
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://github.com/padraic/mockery/blob/master/LICENSE
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Mockery
16
 * @package    Mockery
17
 * @copyright  Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
18
 * @license    http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
19
 */
20
21
namespace Mockery\Generator;
22
23
class Parameter
24
{
25
    private static $parameterCounter;
26
27
    private $rfp;
28
29 49
    public function __construct(\ReflectionParameter $rfp)
30
    {
31 49
        $this->rfp = $rfp;
32 49
    }
33
34 48
    public function __call($method, array $args)
35
    {
36 48
        return call_user_func_array(array($this->rfp, $method), $args);
37
    }
38
39
    public function getClass()
40
    {
41
        return new DefinedTargetClass($this->rfp->getClass());
42
    }
43
44 49
    public function getTypeHintAsString()
45
    {
46 49
        if (method_exists($this->rfp, 'getTypehintText')) {
47
            // Available in HHVM
48
            $typehint = $this->rfp->getTypehintText();
49
50
            // not exhaustive, but will do for now
51
            if (in_array($typehint, array('int', 'integer', 'float', 'string', 'bool', 'boolean'))) {
52
                return '';
53
            }
54
55
            return $typehint;
56
        }
57
58 49
        if ($this->rfp->isArray()) {
59 3
            return 'array';
60
        }
61
62
        /*
63
         * PHP < 5.4.1 has some strange behaviour with a typehint of self and
64
         * subclass signatures, so we risk the regexp instead
65
         */
66 49
        if ((version_compare(PHP_VERSION, '5.4.1') >= 0)) {
67
            try {
68 49
                if ($this->rfp->getClass()) {
69 6
                    return $this->rfp->getClass()->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->rfp->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
70
                }
71 46
            } catch (\ReflectionException $re) {
72
                // noop
73
            }
74 46
        }
75
76 46
        if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0 && $this->rfp->hasType()) {
77
            return (string) $this->rfp->getType();
78
        }
79
80 46
        if (preg_match('/^Parameter #[0-9]+ \[ \<(required|optional)\> (?<typehint>\S+ )?.*\$' . $this->rfp->getName() . ' .*\]$/', $this->rfp->__toString(), $typehintMatch)) {
0 ignored issues
show
Bug introduced by
Consider using $this->rfp->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
81 46
            if (!empty($typehintMatch['typehint'])) {
82 3
                return $typehintMatch['typehint'];
83
            }
84 44
        }
85
86 44
        return '';
87
    }
88
89
    /**
90
     * Some internal classes have funny looking definitions...
91
     */
92 41
    public function getName()
93
    {
94 41
        $name = $this->rfp->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->rfp->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
95 41
        if (!$name || $name == '...') {
96 1
            $name = 'arg' . static::$parameterCounter++;
97 1
        }
98
99 41
        return $name;
100
    }
101
102
103
    /**
104
     * Variadics only introduced in 5.6
105
     */
106 41
    public function isVariadic()
107
    {
108 41
        return $this->rfp->isVariadic();
109
    }
110
}
111