AbstractButtonTwigExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A adminBSBButton() 0 22 4
1
<?php
2
3
/*
4
 * This file is part of the adminbsb-material-design-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\AdminBSBBundle\Twig\Extension\UI;
13
14
use WBW\Bundle\AdminBSBBundle\Button\ButtonRenderer;
15
use WBW\Bundle\AdminBSBBundle\Twig\Extension\RendererTwigExtension;
16
use WBW\Bundle\BootstrapBundle\Button\ButtonInterface;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension as BaseTwigExtension;
18
19
/**
20
 * Abstract button Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\AdminBSBBundle\Twig\Extension\UI
24
 * @abstract
25
 */
26
abstract class AbstractButtonTwigExtension extends BaseTwigExtension {
27
28
    /**
29
     * Displays an AdminBSB button.
30
     *
31
     * @param ButtonInterface $button The button.
32
     * @param string|null $icon The icon.
33
     * @param bool $circle Circle ?
34
     * @return string Returns the AdminBSB button.
35
     */
36
    protected function adminBSBButton(ButtonInterface $button, ?string $icon, bool $circle): string {
37
38
        // Fix some parameters.
39
        $circle = null !== $button->getContent() ? false : $circle;
40
        $style  = null !== $button->getContent() ? "margin: -4px 2px 0; vertical-align: sub;" : "";
41
42
        $attributes = [];
43
44
        $attributes["class"]          = ["btn", ButtonRenderer::renderType($button), "waves-effect"];
45
        $attributes["class"][]        = ButtonRenderer::renderBlock($button);
46
        $attributes["class"][]        = ButtonRenderer::renderCircle($button, $circle);
47
        $attributes["title"]          = ButtonRenderer::renderTitle($button);
48
        $attributes["type"]           = "button";
49
        $attributes["data-toggle"]    = ButtonRenderer::renderDataToggle($button);
50
        $attributes["data-placement"] = ButtonRenderer::renderDataPlacement($button);
51
        $attributes["disabled"]       = ButtonRenderer::renderDisabled($button);
52
53
        $glyphicon = null !== $icon ? RendererTwigExtension::renderIcon($this->getTwigEnvironment(), $icon, $style) : "";
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...
54
        $innerHTML = ButtonRenderer::renderContent($button);
55
56
        return static::coreHTMLElement("button", $glyphicon . $innerHTML, $attributes);
57
    }
58
}
59