Passed
Push — developer ( 57de19...8207c1 )
by Mariusz
35:57
created

EditView   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 17.19 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 11
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPermission() 11 11 3
B process() 0 36 8
A getFooterScripts() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Edit view class.
4
 *
5
 * @package View
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace YF\Modules\Base\View;
14
15
class EditView extends \App\Controller\View
16
{
17
	/** {@inheritdoc} */
18 View Code Duplication
	public function checkPermission(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
	{
20
		parent::checkPermission($this->request);
0 ignored issues
show
Unused Code introduced by
The call to View::checkPermission() has too many arguments starting with $this->request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
21
		$actionName = 'EditView';
22
		if ($this->request->isEmpty('record')) {
23
			$actionName = 'CreateView';
24
		}
25
		if (!\YF\Modules\Base\Model\Module::isPermitted($this->request->getModule(), $actionName)) {
26
			throw new \App\Exceptions\AppException('ERR_MODULE_PERMISSION_DENIED');
27
		}
28
	}
29
30
	/** {@inheritdoc} */
31
	public function process()
32
	{
33
		$moduleName = $this->request->getModule();
34
		if ($this->request->isEmpty('record')) {
35
			$recordModel = \YF\Modules\Base\Model\Record::getInstance($moduleName);
36
		} else {
37
			$recordModel = \YF\Modules\Base\Model\Record::getInstanceById($moduleName, $this->request->getInteger('record'), ['x-raw-data' => 1]);
38
		}
39
		$moduleStructure = $recordModel->getModuleModel()->getFieldsFromApi();
40
		$data = $recordModel->getData();
41
		$rawData = $recordModel->getRawData();
42
		$fields = [];
43
		foreach ($moduleStructure['fields'] as $field) {
44
			$fieldName = $field['name'];
45
			if ($field['isEditable']) {
46
				$fieldInstance = \YF\Modules\Base\Model\Field::getInstance($moduleName, $field);
47
				if (isset($data[$fieldName])) {
48
					$fieldInstance->setDisplayValue($data[$fieldName]);
49
					if (isset($rawData[$fieldName])) {
50
						$fieldInstance->setRawValue($rawData[$fieldName]);
51
					}
52
				} elseif (!empty($field['referenceList']) && 'Accounts' === current($field['referenceList'])) {
53
					$fieldInstance->setDisplayValue(\App\User::getUser()->get('parentName'));
54
					$fieldInstance->setRawValue(\App\User::getUser()->get('companyId'));
55
				} else {
56
					$fieldInstance->setIsNewRecord();
57
				}
58
				$fields[$field['blockId']][] = $fieldInstance;
59
			}
60
		}
61
		$this->viewer->assign('RECORD', $recordModel);
62
		$this->viewer->assign('FIELDS', $fields);
63
		$this->viewer->assign('BREADCRUMB_TITLE', $recordModel->getName());
64
		$this->viewer->assign('BLOCKS', $moduleStructure['blocks']);
65
		$this->viewer->view('Edit/EditView.tpl', $moduleName);
66
	}
67
68
	/** {@inheritdoc} */
69
	public function getFooterScripts(bool $loadForModule = true): array
70
	{
71
		return array_merge(
72
			parent::getFooterScripts(),
73
			$this->convertScripts([
74
				['layouts/' . \App\Viewer::getLayoutName() . '/modules/Base/resources/EditView.js'],
75
			], 'js')
76
		);
77
	}
78
}
79