| Conditions | 6 |
| Paths | 12 |
| Total Lines | 24 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 35 | protected function stub($class, array $methods = [], $className = '', $forceMethods = false) |
||
| 36 | { |
||
| 37 | $builder = $this->getMockBuilder($class) |
||
|
|
|||
| 38 | ->disableOriginalConstructor(); |
||
| 39 | |||
| 40 | if (!empty($methods) && $forceMethods) { |
||
| 41 | $builder->setMethods(array_keys($methods)); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ('' !== $className) { |
||
| 45 | $builder->setMockClassName($className); |
||
| 46 | } |
||
| 47 | |||
| 48 | $stub = $builder->getMock(); |
||
| 49 | foreach ($methods as $method => $value) { |
||
| 50 | if (is_callable($value)) { |
||
| 51 | $stub->expects($this->any())->method($method)->willReturnCallback($value); |
||
| 52 | } else { |
||
| 53 | $stub->expects($this->any())->method($method)->willReturn($value); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return $stub; |
||
| 58 | } |
||
| 59 | } |
||
| 60 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.