File: ParameterBuilder.php

package info (click to toggle)
php-mock 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: php: 1,740; makefile: 18; xml: 17; sh: 7
file content (78 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

namespace phpmock\generator;

use ReflectionFunction;

/**
 * Builder for the mocked function parameters.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @internal
 */
class ParameterBuilder
{
    /**
     * @var string The signature's parameters.
     */
    private $signatureParameters = '';

    /**
     * @var string The body's parameter access list.
     */
    private $bodyParameters = '';

    /**
     * Builds the parameters for an existing function.
     *
     * @param string $functionName The function name.
     */
    public function build($functionName)
    {
        if (!function_exists($functionName)) {
            return;
        }
        $function            = new ReflectionFunction($functionName);
        $signatureParameters = [];
        $bodyParameters      = [];
        foreach ($function->getParameters() as $reflectionParameter) {
            if ($reflectionParameter->isVariadic()) {
                break;
            }
            $parameter = $reflectionParameter->isPassedByReference()
                ? "&$$reflectionParameter->name"
                : "$$reflectionParameter->name";

            $signatureParameter = $reflectionParameter->isOptional()
                ? sprintf("%s = '%s'", $parameter, MockFunctionGenerator::DEFAULT_ARGUMENT)
                : $parameter;

            $signatureParameters[] = $signatureParameter;
            $bodyParameters[]      = $parameter;
        }
        $this->signatureParameters = implode(", ", $signatureParameters);
        $this->bodyParameters      = implode(", ", $bodyParameters);
    }

    /**
     * Returns the signature's parameters.
     *
     * @return string The signature's parameters.
     */
    public function getSignatureParameters()
    {
        return $this->signatureParameters;
    }

    /**
     * Returns the body's parameter access list.
     *
     * @return string The body's parameter list.
     */
    public function getBodyParameters()
    {
        return $this->bodyParameters;
    }
}