Completed
Push — master ( 8523c1...304fd8 )
by Bart
07:18
created

AssetsField   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 35.09 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 10
c 6
b 0
f 2
lcom 0
cbo 1
dl 20
loc 57
ccs 0
cts 30
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getDefinition() 10 21 5
B populate() 10 20 5

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
namespace NerdsAndCompany\Schematic\Models;
4
5
use Craft\Craft;
6
use Craft\FieldModel;
7
use Craft\FieldGroupModel;
8
9
/**
10
 * Schematic Assets Field Model.
11
 *
12
 * A schematic field model for mapping asset data
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @link      http://www.nerds.company
19
 */
20
class AssetsField extends Field
21
{
22
    /**
23
     * @param FieldModel $field
24
     * @param $includeContext
25
     *
26
     * @return array
27
     */
28
    public function getDefinition(FieldModel $field, $includeContext)
29
    {
30
        $definition = parent::getDefinition($field, $includeContext);
31
        $settings = $definition['settings'];
32
33 View Code Duplication
        if (array_key_exists('defaultUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
            $defaultUploadLocationSourceId = $settings['defaultUploadLocationSource'];
35
            $defaultUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeById($defaultUploadLocationSourceId);
36
            $settings['defaultUploadLocationSource'] = $defaultUploadLocationSource ? $defaultUploadLocationSource->handle : '';
37
        }
38
39 View Code Duplication
        if (array_key_exists('singleUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
            $singleUploadLocationSourceId = $settings['singleUploadLocationSource'];
41
            $singleUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeById($singleUploadLocationSourceId);
42
            $settings['singleUploadLocationSource'] = $singleUploadLocationSource ? $singleUploadLocationSource->handle : '';
43
        }
44
45
        $definition['settings'] = $settings;
46
47
        return $definition;
48
    }
49
50
    /**
51
     * @param array                $fieldDefinition
52
     * @param FieldModel           $field
53
     * @param string               $fieldHandle
54
     * @param FieldGroupModel|null $group
55
     */
56
    public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null)
57
    {
58
        parent::populate($fieldDefinition, $field, $fieldHandle, $group);
59
60
        $settings = $fieldDefinition['settings'];
61
62 View Code Duplication
        if (array_key_exists('defaultUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            $defaultUploadLocationSourceId = $settings['defaultUploadLocationSource'];
64
            $defaultUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($defaultUploadLocationSourceId);
65
            $settings['defaultUploadLocationSource'] = $defaultUploadLocationSource ? $defaultUploadLocationSource->id : '';
66
        }
67
68 View Code Duplication
        if (array_key_exists('singleUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $singleUploadLocationSourceId = $settings['singleUploadLocationSource'];
70
            $singleUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($singleUploadLocationSourceId);
71
            $settings['singleUploadLocationSource'] = $singleUploadLocationSource ? $singleUploadLocationSource->id : '';
72
        }
73
74
        $field->settings = $settings;
75
    }
76
}
77