Completed
Pull Request — master (#57)
by Olexandr
02:41
created

Table   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 3
dl 10
loc 95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createUses() 0 8 1
A createDefinition() 10 10 1
A createConstants() 0 10 1
A createStaticFields() 0 6 1
A createConstructor() 0 16 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
 * Created by PhpStorm.
4
 * User: nazarenko
5
 * Date: 29.03.2016
6
 * Time: 11:11
7
 */
8
namespace samsoncms\api\generator;
9
10
use samsoncms\api\generator\metadata\Virtual;
11
use samsonphp\generator\Generator;
12
13
/**
14
 * Table class generator.
15
 *
16
 * @package samsoncms\api\generator
17
 */
18
class Table extends Generic
19
{
20
    /**
21
     * Query constructor.
22
     *
23
     * @param Generator $generator
24
     * @param           $metadata
25
     */
26
    public function __construct(Generator $generator, $metadata)
27
    {
28
        parent::__construct($generator, $metadata);
29
30
        $this->className = rtrim($this->className, 'Table').'Table';
31
    }
32
33
    /**
34
     * Class uses generation part.
35
     *
36
     * @param \samsoncms\api\generator\metadata\Gallery $metadata Entity metadata
37
     */
38
    protected function createUses($metadata)
39
    {
40
        $this->generator
41
            ->newLine('use samsonframework\core\ViewInterface;')
42
            ->newLine('use samsonframework\orm\QueryInterface;')
43
            ->newLine('use samson\activerecord\dbQuery;')
44
            ->newLine();
45
    }
46
47
    /**
48
     * Class definition generation part.
49
     *
50
     * @param Virtual $metadata Entity metadata
51
     */
52 View Code Duplication
    protected function createDefinition($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...
53
    {
54
        $this->generator
55
            ->multiComment(array(
56
                'Class for rendering "' . $metadata->entityRealName . '" table',
57
            ))
58
            ->defClass($this->className, '\\'.\samsoncms\api\field\Table::class)
59
            ->newLine('use \\'.\samsoncms\api\Renderable::class.';')
60
            ->newLine();
61
    }
62
63
    /**
64
     * Class constants generation part.
65
     *
66
     * @param Virtual $metadata Entity metadata
67
     */
68
    protected function createConstants($metadata)
69
    {
70
        $this->generator
71
            ->commentVar('string', 'Entity full class name, use ::class instead')
72
            ->defClassConst('ENTITY', $metadata->entityClassName)
73
            ->commentVar('string', 'Entity database identifier')
74
            ->defClassConst('IDENTIFIER', $metadata->entityID)
75
            ->commentVar('string', 'Not transliterated entity name')
76
            ->defClassConst('NAME', $metadata->entityRealName);
77
    }
78
79
    /**
80
     * Class static fields generation part.
81
     *
82
     * @param Virtual $metadata Entity metadata
83
     */
84
    protected function createStaticFields($metadata)
85
    {
86
        $this->generator
87
            ->commentVar('array', 'Collection of real additional field names')
88
            ->defClassVar('$fieldsRealNames', 'public static', $metadata->realNames);
89
    }
90
91
    /**
92
     * Class constructor generation part.
93
     *
94
     * @param Virtual $metadata Entity metadata
95
     */
96
    protected function createConstructor($metadata)
97
    {
98
        $class = "\n\t" . '/**';
99
        $class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance';
100
        $class .= "\n\t" . ' * @param int $materialID material identifier';
101
        $class .= "\n\t" . ' * @param QueryInterface $query Database query instance';
102
        $class .= "\n\t" . ' * @param string $locale locale';
103
        $class .= "\n\t" . ' */';
104
        $class .= "\n\t" . 'public function __construct(ViewInterface $renderer, $materialID, QueryInterface $query = null, $locale = null)';
105
        $class .= "\n\t" . '{';
106
        $class .= "\n\t\t" . '$this->renderer = $renderer;';
107
        $class .= "\n\t\t" . 'parent::__construct(null !== $query ? $query : new dbQuery(), array('. $metadata->entityID .'), $materialID, $locale);';
108
        $class .= "\n\t" . '}' . "\n";
109
110
        $this->generator->text($class);
111
    }
112
}