CounterComponent::onRender()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
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 18.

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 Counter Ajax
4
 * Webino Example
5
 */
6
7
use WebinoAppLib\Event\RouteEvent;
8
use WebinoAppLib\Response\ViewResponse;
9
use WebinoAppLib\Router\DefaultRoute;
10
use WebinoAppLib\View\SourcePreviewComponent;
11
use WebinoDomLib\Event\RenderEvent;
12
use WebinoViewLib\Component\AbstractViewComponent;
13
use WebinoViewLib\Component\Javascript;
14
use WebinoViewLib\Component\Stylesheet;
15
use WebinoViewLib\Feature\CommonView;
16
use WebinoViewLib\Feature\NodeView;
17
18
require __DIR__ . '/../../vendor/autoload.php';
19
20
/**
21
 * Custom view component
22
 */
23 View Code Duplication
class CounterComponent extends AbstractViewComponent
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
{
25
    public function initState(\stdClass $state)
26
    {
27
        $state->count = 0;
28
    }
29
30
    public function configure(NodeView $node)
31
    {
32
        $node
33
            ->setLocator('counter')
34
            ->setReplace('<div class="ajax-fragment btn-group"><display/><plus/><minus/></div>')
35
            ->setView([
36
                (new NodeView('display'))
37
                    ->setRename('span')
38
                    ->setAddClass('btn btn-default disabled'),
39
40
                (new NodeView('plus'))
41
                    ->setRename('a')
42
                    ->setAddClass('btn btn-info ajax')
43
                    ->setValue('+'),
44
45
                (new NodeView('minus'))
46
                    ->setRename('a')
47
                    ->setAddClass('btn btn-info ajax')
48
                    ->setValue('-'),
49
            ]);
50
    }
51
52
    public function onRender(RenderEvent $event)
53
    {
54
        $event->getNode('display')
55
            ->setValue($this->getState()->count);
56
57
        $this->onStateChange('plus', function () {
58
            $this->getState()->count++;
59
        });
60
61
        $this->onStateChange('minus', function () {
62
            $this->getState()->count--;
63
        });
64
    }
65
}
66
67
$config = Webino::config([
68
    new CommonView([
69
        new Stylesheet\BootstrapV3,
70
        new Javascript\WebinoAjaxJQueryV0,
71
        new SourcePreviewComponent(__FILE__),
72
73
        (new NodeView('content'))
74
            ->setLocator('body')
75
            ->setHtml('<div class="container jumbotron"><counter/></div><source-preview/>'),
76
77
        new CounterComponent,
78
    ]),
79
]);
80
81
$app = Webino::application($config)->bootstrap();
82
83
$app->bind(DefaultRoute::class, function (RouteEvent $event) {
84
    $event->setResponse(new ViewResponse);
85
});
86
87
$app->dispatch();
88