Test Failed
Push — develop ( db5b9c...b55dd4 )
by Paul
17:52
created

RenderCallbackTrait::render_callback()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 98
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 98
rs 8.9818
cc 1
nc 1
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * StaticModule::render_callback()
4
 *
5
 * @package MEE\Modules\StaticModule
6
 * @since ??
7
 */
8
9
namespace MEE\Modules\StaticModule\StaticModuleTrait;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	die( 'Direct access forbidden.' );
13
}
14
15
// phpcs:disable ET.Sniffs.ValidVariableName.UsedPropertyNotSnakeCase -- WP use snakeCase in \WP_Block_Parser_Block
16
17
use ET\Builder\Packages\Module\Module;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Packages\Module\Module was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use ET\Builder\Framework\Utility\HTMLUtility;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Framework\Utility\HTMLUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use ET\Builder\FrontEnd\BlockParser\BlockParserStore;
0 ignored issues
show
Bug introduced by
The type ET\Builder\FrontEnd\BlockParser\BlockParserStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use ET\Builder\Packages\Module\Options\Element\ElementComponents;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Packages\Modu...ement\ElementComponents was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use MEE\Modules\StaticModule\StaticModule;
22
23
trait RenderCallbackTrait {
24
25
	/**
26
	 * Static module render callback which outputs server side rendered HTML on the Front-End.
27
	 *
28
	 * @since ??
29
	 * @param array          $attrs    Block attributes that were saved by VB.
30
	 * @param string         $content  Block content.
31
	 * @param WP_Block       $block    Parsed block object that being rendered.
0 ignored issues
show
Bug introduced by
The type MEE\Modules\StaticModule...ticModuleTrait\WP_Block was not found. Did you mean WP_Block? If so, make sure to prefix the type with \.
Loading history...
32
	 * @param ModuleElements $elements ModuleElements instance.
0 ignored issues
show
Bug introduced by
The type MEE\Modules\StaticModule...uleTrait\ModuleElements was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
	 *
34
	 * @return string HTML rendered of Static module.
35
	 */
36
	public static function render_callback( $attrs, $content, $block, $elements ) {
37
		// Image.
38
		$image_src = $attrs['image']['innerContent']['desktop']['value']['src'] ?? '';
39
		$image_alt = $attrs['image']['innerContent']['desktop']['value']['alt'] ?? '';
40
		$image     = HTMLUtility::render(
41
			[
42
				'tag'                  => 'img',
43
				'attributes'           => [
44
					'src' => $image_src,
45
					'alt' => $image_alt,
46
				],
47
				'attributesSanitizers' => [
48
					'src' => function ( $value ) {
49
						$protocols = array_merge( wp_allowed_protocols(), [ 'data' ] ); // Need to add `data` protocol for default image.
50
						return esc_url( $value, $protocols );
51
					},
52
				],
53
			]
54
		);
55
56
		// Image container.
57
		$image_container = HTMLUtility::render(
58
			[
59
				'tag'               => 'div',
60
				'attributes'        => [
61
					'class' => 'example_static_module__image',
62
				],
63
				'childrenSanitizer' => 'et_core_esc_previously',
64
				'children'          => $image,
65
			]
66
		);
67
68
		// Title.
69
		$title = $elements->render(
70
			[
71
				'attrName' => 'title',
72
			]
73
		);
74
75
		// Content.
76
		$content = $elements->render(
77
			[
78
				'attrName' => 'content',
79
			]
80
		);
81
82
		// Content container.
83
		$content_container = HTMLUtility::render(
84
			[
85
				'tag'               => 'div',
86
				'attributes'        => [
87
					'class' => 'example_static_module__content-container',
88
				],
89
				'childrenSanitizer' => 'et_core_esc_previously',
90
				'children'          => $title . $content,
91
			]
92
		);
93
94
		$parent       = BlockParserStore::get_parent( $block->parsed_block['id'], $block->parsed_block['storeInstance'] );
95
		$parent_attrs = $parent->attrs ?? [];
96
97
		return Module::render(
98
			[
99
				// FE only.
100
				'orderIndex'          => $block->parsed_block['orderIndex'],
101
				'storeInstance'       => $block->parsed_block['storeInstance'],
102
103
				// VB equivalent.
104
				'attrs'               => $attrs,
105
				'elements'            => $elements,
106
				'id'                  => $block->parsed_block['id'],
107
				'name'                => $block->block_type->name,
108
				'moduleCategory'      => $block->block_type->category,
109
				'classnamesFunction'  => [ StaticModule::class, 'module_classnames' ],
110
				'stylesComponent'     => [ StaticModule::class, 'module_styles' ],
111
				'scriptDataComponent' => [ StaticModule::class, 'module_script_data' ],
112
				'parentAttrs'         => $parent_attrs,
113
				'parentId'            => $parent->id ?? '',
114
				'parentName'          => $parent->blockName ?? '',
115
				'children'            => [
116
					ElementComponents::component(
117
						[
118
							'attrs'         => $attrs['module']['decoration'] ?? [],
119
							'id'            => $block->parsed_block['id'],
120
121
							// FE only.
122
							'orderIndex'    => $block->parsed_block['orderIndex'],
123
							'storeInstance' => $block->parsed_block['storeInstance'],
124
						]
125
					),
126
					HTMLUtility::render(
127
						[
128
							'tag'               => 'div',
129
							'attributes'        => [
130
								'class' => 'example_static_module__inner',
131
							],
132
							'childrenSanitizer' => 'et_core_esc_previously',
133
							'children'          => $image_container . $content_container,
134
						]
135
					),
136
				],
137
			]
138
		);
139
	}
140
}
141