CoffeeMachineComponent   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 4 1
A configure() 0 17 1
A onBuyCoffee() 0 5 1
A onDispatch() 0 7 1
A getInitialState() 0 4 1
A onRender() 0 7 1
A init() 0 8 1
A toArray() 0 6 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * View Component Coffee Machine
4
 * Webino Example
5
 */
6
7
use WebinoAppLib\Event\RouteEvent;
8
use WebinoAppLib\Response\Content\SourcePreview;
9
use WebinoAppLib\Response\ViewResponse;
10
use WebinoAppLib\Router\DefaultRoute;
11
use WebinoConfigLib\Feature\Route;
12
use WebinoDomLib\Event\RenderEvent;
13
use WebinoViewLib\Component\AbstractViewComponent;
14
use WebinoViewLib\Component\OnRenderComponentInterface;
15
use WebinoViewLib\Feature\CommonView;
16
use WebinoViewLib\Feature\NodeView;
17
use WebinoViewLib\Feature\ViewTemplateMap;
18
19
require __DIR__ . '/../../vendor/autoload.php';
20
21
// TODO
22
23
class CoffeeMachineComponent extends AbstractViewComponent implements
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
    OnRenderComponentInterface,
25
    \WebinoViewLib\Component\OnDispatchInterface,
26
    \WebinoAppLib\Service\Initializer\RoutingAwareInterface
27
{
28
    use WebinoAppLib\Service\Initializer\RoutingAwareTrait;
29
    use \WebinoAppLib\Listener\RouteListenerTrait;
30
31
    private function getIndex()
32
    {
33
        return 0;
34
    }
35
36
    public function configure(NodeView $node)
37
    {
38
        $node
39
            ->setLocator('coffee-machine')
40
            ->setSnippet('snippet', 'my/snippets/coffee-machine')
41
            ->setReplace('{$snippet}')
42
            ->setView([
43
                (new NodeView('display'))
44
                    ->setRename('p')
45
                    ->setValue('Coffee Machine Display'),
46
47
                (new NodeView('buy'))
48
                    ->setRename('a')
49
                    ->setAttribute('href', '#')
50
                    ->setValue('Buy coffee'),
51
            ]);
52
    }
53
54
    public function onBuyCoffee(RouteEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        // TODO concept
57
        $this->setState();
0 ignored issues
show
Bug introduced by
The call to setState() misses a required argument $state.

This check looks for function calls that miss required arguments.

Loading history...
58
    }
59
60
    public function onDispatch(\WebinoAppLib\Event\DispatchEvent $event)
61
    {
62
        // TODO
63
        $state = $event->getRequest()->getQuery()->coffeeMachine[$this->getIndex()];
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getQuery() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
65
        dd($state);
66
    }
67
68
    // TODO concept
69
    public function getInitialState()
70
    {
71
72
    }
73
74
    public function onRender(RenderEvent $event)
75
    {
76
        // TODO common
77
        $href = $this->getRouter()->url(null)->setOption('query', ['coffeeMachine' => [$this->getIndex() => ['bought' => 1]]]);
78
//        die($href);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
        $event->getNode('buy')->setAttribute('href', $href);
80
    }
81
82
    /**
83
     * Initialize listener
84
     */
85
    protected function init()
86
    {
87
        parent::init();
88
89
        // TODO common
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
//        $this->listenRoute('coffeeMachineBuy', 'onBuyCoffee');
91
//        $this->listen(RouteEvent::MATCH, 'onBuyCoffee');
92
    }
93
94
95
    /**
96
     * @return array
97
     */
98
    public function toArray()
99
    {
100
        // TODO common
101
        return parent::toArray();
102
//        return parent::toArray() + (new Route('coffeeMachineBuy'))->setLiteral('/coffee-machine-buy')->toArray();
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
    }
104
}
105
106
$config = Webino::config([
107
    /**
108
     * Configuring view
109
     * response route.
110
     */
111
    (new Route('viewTest'))->setLiteral('/view-test'),
112
113
    new ViewTemplateMap(__DIR__ . '/view'),
114
115
    new CommonView([
116
        (new NodeView('content'))
117
            ->setLocator('body')
118
            ->setHtml('<coffee-machine/>'),
119
120
        new CoffeeMachineComponent,
121
    ]),
122
]);
123
124
$app = Webino::application($config)->bootstrap();
125
126
$app->bindRoute('viewTest', function (RouteEvent $event) {
127
    /**
128
     * Responding
129
     * using view.
130
     */
131
    $event->setResponse(new ViewResponse);
132
});
133
134
$app->bind(DefaultRoute::class, function (RouteEvent $event) {
135
    $event->setResponse([
136
        $event->getApp()->url('viewTest')->html('View response!'),
137
        new SourcePreview(__FILE__),
138
    ]);
139
});
140
141
$app->dispatch();
142