Completed
Push — master ( 0c7c34...b7f628 )
by
unknown
02:49
created

VirtualQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 17.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 15
loc 87
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createUses() 0 8 1
A __construct() 0 7 1
B createStaticFields() 0 30 1
A createConstructor() 15 15 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
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 22.03.16 at 15:46
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsoncms\api\generator\metadata\VirtualMetadata;
10
use samsoncms\api\query\Entity;
11
use samsonphp\generator\Generator;
12
13
/**
14
 * Virtual entity query class generator.
15
 *
16
 * @package samsoncms\api\generator
17
 */
18
class VirtualQuery extends RealQuery
19
{
20
    /**
21
     * Class uses generation part.
22
     *
23
     * @param VirtualMetadata $metadata Entity metadata
24
     */
25
    protected function createUses($metadata)
26
    {
27
        $this->generator
28
            ->newLine('use samsonframework\orm\QueryInterface;')
29
            ->newLine('use samsonframework\orm\ArgumentInterface;')
30
            ->newLine('use samson\activerecord\dbQuery;')
31
            ->newLine();
32
    }
33
34
    /**
35
     * Query constructor.
36
     *
37
     * @param Generator $generator
38
     * @param VirtualMetadata $metadata
39
     */
40
    public function __construct(Generator $generator, $metadata)
41
    {
42
        parent::__construct($generator, $metadata);
43
44
        $this->parentClass = '\\'.Entity::class;
45
        $this->entityClass = '\samsoncms\api\generated\\' . $metadata->entity;
46
    }
47
48
    /**
49
     * Class static fields generation part.
50
     *
51
     * @param VirtualMetadata $metadata Entity metadata
52
     */
53
    protected function createStaticFields($metadata)
54
    {
55
        $this->generator
56
            ->commentVar('array', 'Collection of localized additional fields identifiers')
57
            ->defClassVar('$virtualFieldIDs', 'public static', $metadata->fields)
58
            ->commentVar('array', 'Collection of additional fields value column names')
59
            ->defClassVar('$virtualFieldValueColumns', 'public static', $metadata->allFieldValueColumns)
60
            ->commentVar('array', 'Collection of real additional field names')
61
            ->defClassVar('$virtualFieldRealNames', 'public static', $metadata->realNames)
62
            ->commentVar('array', 'Collection of additional field names')
63
            ->defClassVar('$virtualFieldNames', 'public static', $metadata->fieldNames)
64
            ->commentVar('array', 'Collection of additional field names')
65
            ->defClassVar('$virtualRealFieldNames', 'public static', $metadata->fieldNames)
66
67
            ->commentVar('array', 'Collection of localized additional fields identifiers')
68
            ->defClassVar('$fieldIDs', 'public static', $metadata->parent->fields)
69
            ->commentVar('array', 'Collection of additional field names')
70
            ->defClassVar('$fieldNames', 'public static', $metadata->parent->fieldNames)
71
72
            // TODO: two above fields should be protected
73
            ->commentVar('array', 'Collection of navigation identifiers')
74
            ->defClassVar('$navigationIDs', 'public static', array($metadata->entityID))
75
            ->commentVar('string', 'Entity full class name')
76
            ->defClassVar('$identifier', 'public static', $this->entityClass)
77
            ->commentVar('array', 'Collection of localized additional fields identifiers')
78
            ->defClassVar('$localizedFieldIDs', 'public static', $metadata->localizedFieldIDs)
79
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
80
            ->defClassVar('$notLocalizedFieldIDs', 'public static', $metadata->notLocalizedFieldIDs)
81
        ;
82
    }
83
84
    /**
85
     * Class constructor generation part.
86
     *
87
     * @param \samsoncms\api\generator\metadata\VirtualMetadata $metadata Entity metadata
88
     */
89 View Code Duplication
    protected function createConstructor($metadata)
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...
90
    {
91
        $class = "\n\t" . '/**';
92
        $class .= "\n\t" . ' * @param string $locale Localization identifier';
93
        $class .= "\n\t" . ' * @param QueryInterface $query Database query instance';
94
        $class .= "\n\t" . ' */';
95
        $class .= "\n\t" . 'public function __construct($locale = null, QueryInterface $query = null)';
96
        $class .= "\n\t" . '{';
97
        $class .= "\n\t\t" . '// TODO: This should be removed!';
98
        $class .= "\n\t\t" . '$container = $GLOBALS[\'__core\']->getContainer();';
99
        $class .= "\n\t\t" . 'parent::__construct($query ?? $container->get("query"), $locale);';
100
        $class .= "\n\t" . '}';
101
102
        $this->generator->text($class);
103
    }
104
}
105
//[PHPCOMPRESSOR(remove,end)]
106