AbstractButtonTwigExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A bootstrapButton() 0 19 2
1
<?php
2
3
/*
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\CSS;
13
14
use WBW\Bundle\BootstrapBundle\Button\ButtonInterface;
15
use WBW\Bundle\BootstrapBundle\Button\ButtonRenderer;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractTwigExtension;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\RendererTwigExtension;
18
19
/**
20
 * Abstract button Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\CSS
24
 * @abstract
25
 */
26
abstract class AbstractButtonTwigExtension extends AbstractTwigExtension {
27
28
    /**
29
     * Displays a Bootstrap button.
30
     *
31
     * @param ButtonInterface $button The button.
32
     * @param string|null $icon The icon.
33
     * @return string Returns the Bootstrap button.
34
     */
35
    protected function bootstrapButton(ButtonInterface $button, ?string $icon): string {
36
37
        $attributes = [];
38
39
        $attributes["class"]          = ["btn", ButtonRenderer::renderType($button)];
40
        $attributes["class"][]        = ButtonRenderer::renderBlock($button);
41
        $attributes["class"][]        = ButtonRenderer::renderSize($button);
42
        $attributes["class"][]        = ButtonRenderer::renderActive($button);
43
        $attributes["title"]          = ButtonRenderer::renderTitle($button);
44
        $attributes["type"]           = "button";
45
        $attributes["data-toggle"]    = ButtonRenderer::renderDataToggle($button);
46
        $attributes["data-placement"] = ButtonRenderer::renderDataPLacement($button);
47
        $attributes["disabled"]       = ButtonRenderer::renderDisabled($button);
48
49
        $glyphicon = null !== $icon ? RendererTwigExtension::renderIcon($this->getTwigEnvironment(), $icon) : "";
0 ignored issues
show
Bug introduced by
It seems like $this->getTwigEnvironment() can be null; however, renderIcon() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
50
        $innerHTML = ButtonRenderer::renderContent($button);
51
52
        return static::coreHTMLElement("button", implode(" ", [$glyphicon, $innerHTML]), $attributes);
53
    }
54
}
55