Completed
Push — master ( 73c6ff...eb27a4 )
by Marc
03:03
created

StubTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B stub() 0 24 6
1
<?php
2
/*
3
 * This file is part of the phpflo/core package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PhpFlo\Test;
11
12
/**
13
 * Class StubTrait
14
 *
15
 * @package PhpFlo\PhpFloBundle\Test
16
 * @author Marc Aschmann <[email protected]>
17
 */
18
trait StubTrait
19
{
20
    /**
21
     * Will create a stub with several methods and defined return values.
22
     * definition:
23
     * [
24
     *   'myMethod' => 'somevalue',
25
     *   'myOtherMethod' => $callback,
26
     *   'anotherMethod' => function ($x) use ($y) {},
27
     * ]
28
     *
29
     * @param string $class
30
     * @param array $methods
31
     * @param string $className classname for mock object
32
     * @param bool $forceMethods
33
     * @return \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    protected function stub($class, array $methods = [], $className = '', $forceMethods = false)
36
    {
37
        $builder = $this->getMockBuilder($class)
0 ignored issues
show
Bug introduced by
It seems like getMockBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like any() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
52
            } else {
53
                $stub->expects($this->any())->method($method)->willReturn($value);
0 ignored issues
show
Bug introduced by
It seems like any() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
54
            }
55
        }
56
57
        return $stub;
58
    }
59
}
60