1<?php
2
3/*
4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 *     Marcello Duarte <marcello.duarte@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Prophecy\Call;
13
14use Exception;
15use Prophecy\Argument\ArgumentsWildcard;
16
17/**
18 * Call object.
19 *
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 */
22class Call
23{
24    private $methodName;
25    private $arguments;
26    private $returnValue;
27    private $exception;
28    private $file;
29    private $line;
30    private $scores;
31
32    /**
33     * Initializes call.
34     *
35     * @param string      $methodName
36     * @param array       $arguments
37     * @param mixed       $returnValue
38     * @param Exception   $exception
39     * @param null|string $file
40     * @param null|int    $line
41     */
42    public function __construct($methodName, array $arguments, $returnValue,
43                                Exception $exception = null, $file, $line)
44    {
45        $this->methodName  = $methodName;
46        $this->arguments   = $arguments;
47        $this->returnValue = $returnValue;
48        $this->exception   = $exception;
49        $this->scores      = new \SplObjectStorage();
50
51        if ($file) {
52            $this->file = $file;
53            $this->line = intval($line);
54        }
55    }
56
57    /**
58     * Returns called method name.
59     *
60     * @return string
61     */
62    public function getMethodName()
63    {
64        return $this->methodName;
65    }
66
67    /**
68     * Returns called method arguments.
69     *
70     * @return array
71     */
72    public function getArguments()
73    {
74        return $this->arguments;
75    }
76
77    /**
78     * Returns called method return value.
79     *
80     * @return null|mixed
81     */
82    public function getReturnValue()
83    {
84        return $this->returnValue;
85    }
86
87    /**
88     * Returns exception that call thrown.
89     *
90     * @return null|Exception
91     */
92    public function getException()
93    {
94        return $this->exception;
95    }
96
97    /**
98     * Returns callee filename.
99     *
100     * @return string
101     */
102    public function getFile()
103    {
104        return $this->file;
105    }
106
107    /**
108     * Returns callee line number.
109     *
110     * @return int
111     */
112    public function getLine()
113    {
114        return $this->line;
115    }
116
117    /**
118     * Returns short notation for callee place.
119     *
120     * @return string
121     */
122    public function getCallPlace()
123    {
124        if (null === $this->file) {
125            return 'unknown';
126        }
127
128        return sprintf('%s:%d', $this->file, $this->line);
129    }
130
131    /**
132     * Adds the wildcard match score for the provided wildcard.
133     *
134     * @param ArgumentsWildcard $wildcard
135     * @param false|int $score
136     *
137     * @return $this
138     */
139    public function addScore(ArgumentsWildcard $wildcard, $score)
140    {
141        $this->scores[$wildcard] = $score;
142
143        return $this;
144    }
145
146    /**
147     * Returns wildcard match score for the provided wildcard. The score is
148     * calculated if not already done.
149     *
150     * @param ArgumentsWildcard $wildcard
151     *
152     * @return false|int False OR integer score (higher - better)
153     */
154    public function getScore(ArgumentsWildcard $wildcard)
155    {
156        if (isset($this->scores[$wildcard])) {
157            return $this->scores[$wildcard];
158        }
159
160        return $this->scores[$wildcard] = $wildcard->scoreArguments($this->getArguments());
161    }
162}
163