Completed
Push — master ( 2fd08f...30da5f )
by WEBEWEB
04:40 queued 51s
created

FormRenderer::render()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 5.3846
cc 8
eloc 15
nc 10
nop 2
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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\Library\Core\Form\Renderer;
13
14
use WBW\Library\Core\Sort\Tree\Alphabetical\AlphabeticalTreeSort;
15
use WBW\Library\Core\Sort\Tree\Alphabetical\AlphabeticalTreeSortInterface;
16
17
/**
18
 * Form renderer.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Form\Renderer
22
 * @final
23
 */
24
final class FormRenderer {
25
26
	/**
27
	 * Render a select option.
28
	 *
29
	 * @param mixed $option The option.
30
	 * @param TranslatorInterface $translator The translator service.
31
	 * @return string Returns the label.
32
	 */
33
	public static function render($option, $translator = null) {
34
35
		// Check the option.
36
		if (null === $option) {
37
			return null !== $translator ? $translator->trans("label.empty_selection") : "Empty selection";
38
		}
39
40
		// Check the implementation.
41
		if (true === ($option instanceof TranslatedChoiceRendererInterface)) {
42
			$output = $option->getTranslatedChoiceLabel($translator);
0 ignored issues
show
Bug introduced by
It seems like $translator defined by parameter $translator on line 33 can be null; however, WBW\Library\Core\Form\Re...TranslatedChoiceLabel() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
43
		} else if (true === ($option instanceof TranslateFormRendererInterface)) { // deprecated
44
			$output = $option->getChoiceLabel($translator);
0 ignored issues
show
Bug introduced by
It seems like $translator defined by parameter $translator on line 33 can be null; however, WBW\Library\Core\Form\Re...rface::getChoiceLabel() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
45
		} else if (true === ($option instanceof ChoiceRendererInterface || $option instanceof FormRendererInterface)) {
46
			$output = $option->getChoiceLabel();
47
		} else {
48
			$output = "FormRendererInterface not implemented by this object";
49
		}
50
51
		if (true === ($option instanceof AlphabeticalTreeSortInterface)) {
52
			$multiplier	 = AlphabeticalTreeSort::getLevel($option);
53
			$output		 = implode("", [str_repeat("&nbsp;", $multiplier), "&#9500;&nbsp;", $output]);
54
		}
55
56
		// Return the output.
57
		return $output;
58
	}
59
60
}
61