EditBlock   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 112
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add_assets() 0 7 1
A lazy_get_values() 0 9 1
B lazy_get_children() 0 85 5
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
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 Icybee\Modules\Files\Block;
13
14
use ICanBoogie\I18n;
15
use ICanBoogie\Operation;
16
17
use Brickrouge\Element;
18
use Brickrouge\Document;
19
use Brickrouge\Form;
20
21
use Icybee\Modules\Files as Root;
22
use Icybee\Modules\Files\File;
23
24
/**
25
 * @property File $record
26
 */
27
class EditBlock extends \Icybee\Modules\Nodes\Block\EditBlock
28
{
29
	const ACCEPT = '#files-accept';
30
	const UPLOADER_CLASS = 'uploader class';
31
32
	protected $accept = null;
33
	protected $uploader_class = 'Icybee\Modules\Files\FileUpload';
34
35
	static protected function add_assets(Document $document)
36
	{
37
		parent::add_assets($document);
38
39
		$document->css->add(Root\DIR . 'public/edit.css');
40
		$document->js->add(Root\DIR . 'public/edit.js');
41
	}
42
43
	protected function lazy_get_values()
44
	{
45
		return parent::lazy_get_values() + [
46
47
			File::NID => null,
48
			File::HTTP_FILE => null
49
50
		];
51
	}
52
53
	protected function lazy_get_children()
54
	{
55
		$folder = \ICanBoogie\REPOSITORY . 'tmp';
56
57
		if (!is_writable($folder))
58
		{
59
			return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(\Brickrouge...folder' => $folder)))); (array<*,array>) is incompatible with the return type of the parent method Icybee\Modules\Nodes\Blo...lock::lazy_get_children of type array<*,Icybee\Modules\N...ull|Brickrouge\Element>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
61
				Element::CHILDREN => [
62
63
					$this->t('The folder %folder is not writable !', [ '%folder' => $folder ])
64
65
				]
66
			];
67
		}
68
69
		$properties = $this->values;
70
		$nid = $properties[File::NID];
71
		$path = \ICanBoogie\strip_root($properties[File::HTTP_FILE]);
72
73
		if (!$path && $this->record)
74
		{
75
			try
76
			{
77
				$path = $this->record->pathname->relative;
78
			}
79
			catch (\Exception $e)
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...or($e->getMessage()); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
			{
81
				#
82
				# the associated file might have disappeared
83
				#
84
85
				\ICanBoogie\log_error($e->getMessage());
86
			}
87
		}
88
89
		$this->attributes = \ICanBoogie\array_merge_recursive($this->attributes, [
90
91
			Form::HIDDENS => [
92
93
				File::HTTP_FILE => $path
94
95
			],
96
97
			Form::VALUES => [
98
99
				File::HTTP_FILE => $path
100
101
			]
102
103
		]);
104
105
		$options = [
106
107
			self::ACCEPT => $this->accept,
108
			self::UPLOADER_CLASS => $this->uploader_class
109
110
		];
111
112
		$uploader_class = $options[self::UPLOADER_CLASS];
113
114
		return array_merge(parent::lazy_get_children(), [
115
116
			File::HTTP_FILE => new $uploader_class([
117
118
				Form::LABEL => 'file',
0 ignored issues
show
Deprecated Code introduced by
The constant Brickrouge\Form::LABEL has been deprecated.

This class constant has been deprecated.

Loading history...
119
				Element::REQUIRED => empty($nid),
120
				\Brickrouge\File::FILE_WITH_LIMIT => $this->app->site->metas[$this->module->flat_id . '.max_file_size'],
121
				Element::WEIGHT => -100,
122
				\Brickrouge\File::T_UPLOAD_URL => Operation::encode($this->module->id . '/upload'),
123
124
				'value' => $path
125
126
			]),
127
128
			File::DESCRIPTION => $this->app->editors['rte']->from([
129
130
				Form::LABEL => 'description',
0 ignored issues
show
Deprecated Code introduced by
The constant Brickrouge\Form::LABEL has been deprecated.

This class constant has been deprecated.

Loading history...
131
				Element::WEIGHT => 50,
132
133
				'rows' => 5
134
135
			])
136
		]);
137
	}
138
}
139