Completed
Push — master ( 6f3a07...3c92b6 )
by Mikhail
03:51
created

StopwatchEventEmulate::stop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Dolgov_M <[email protected]>
4
 * @date   15.11.2017 at 16:41
5
 */
6
7
namespace SilexPinbaProvider\Test;
8
9
use Intaro\PinbaBundle\Stopwatch\Stopwatch;
10
use PHPUnit\Framework\TestCase;
11
use Silex\Provider\TwigServiceProvider;
12
use SilexPinbaProvider\SilexPinbaProvider;
13
use Silex\Application;
14
15
class PinbaTest extends TestCase
16
{
17
18
    public function testTwigExtension()
19
    {
20
21
22
        $storage = new \ArrayObject();
23
24
        $app      = new ApplicationEmulator();
25
        $app
26
            ->register(new TwigServiceProvider(),array(
27
                'twig.templates' => array('hello' => 'Hello {{ name }}!'),
28
            ))
29
            ->register(new SilexPinbaProvider());
30
31
        $app['intaro_pinba.stopwatch.class'] = 'SilexPinbaProvider\Test\StopwatchEmulate';
32
        $app->boot();
33
        $app['intaro_pinba.stopwatch'] -> setStorage($storage);
34
        $app->renderView('hello');
35
        $this->assertTrue(is_array($storage['tags']), var_export($storage, true));
36
    }
37
}
38
39
40
class StopwatchEmulate extends Stopwatch
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
41
{
42
43
    /**
44
     * @var \ArrayObject
45
     */
46
    private $storage;
47
48
    /**
49
     * @param \ArrayObject $storage
50
     * @return $this
51
     */
52
    public function setStorage($storage)
53
    {
54
        $this->storage = $storage;
55
        return $this;
56
    }
57
58
59
60
    public function start(array $tags)
61
    {
62
        return new StopwatchEventEmulate($tags, $this->storage);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SilexPinbaPr...$tags, $this->storage); (SilexPinbaProvider\Test\StopwatchEventEmulate) is incompatible with the return type of the parent method Intaro\PinbaBundle\Stopwatch\Stopwatch::start of type Intaro\PinbaBundle\Stopwatch\StopwatchEvent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
    }
64
65
}
66
67
class StopwatchEventEmulate
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
68
{
69
    /**
70
     * @var array
71
     */
72
    private $tags;
73
    /**
74
     * @var \ArrayObject
75
     */
76
    private $storage;
77
78
    /**
79
     * StopwatchEventEmulate constructor.
80
     * @param array $tags
81
     * @param \ArrayObject $storage
82
     */
83
    public function __construct(array $tags, \ArrayObject $storage)
84
    {
85
        $this->tags    = $tags;
86
        $this->storage = $storage;
87
    }
88
89
90
    public function stop()
91
    {
92
       $this->storage['tags'] = $this->tags;
93
    }
94
95
}
96
97
98
class ApplicationEmulator extends Application
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
99
{
100
    use Application\TwigTrait;
101
}