Completed
Pull Request — master (#21)
by Dennis
21:11 queued 02:37
created

TableConfiguration   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 5.1 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 5
loc 98
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getColumns() 0 4 1
A getAllTables() 0 8 2
A __construct() 5 10 2
A getColumnConfiguration() 0 15 3
A getTitle() 0 4 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
namespace TildBJ\Seeder\Provider;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 Dennis Römmich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use TildBJ\Seeder\Traits;
28
29
/**
30
 * TableConfiguration
31
 *
32
 * @author Dennis Römmich<[email protected]>
33
 * @copyright Copyright belongs to the respective authors
34
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
35
 */
36
class TableConfiguration
37
{
38
    use Traits\Language;
39
40
    /**
41
     * extensionConfiguration
42
     *
43
     * @var array $tableConfiguration
44
     */
45
    protected $tableConfiguration;
46
47
    /**
48
     * table
49
     *
50
     * @var string $name
51
     */
52
    protected $name;
53
54
    /**
55
     * ExtensionConfigurationUtility constructor.
56
     *
57
     * @param $tableName
58
     */
59 16
    public function __construct($tableName)
60
    {
61 16 View Code Duplication
        if (!isset($GLOBALS['TCA'][$tableName])) {
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...
62 1
            throw new \InvalidArgumentException(
63 1
                'Configuration for table ' . $tableName . ' not found!'
64
            );
65
        }
66 16
        $this->name = $tableName;
67 16
        $this->tableConfiguration = $GLOBALS['TCA'][$tableName];
68 16
    }
69
70
    /**
71
     * getName
72
     *
73
     * @return string
74
     */
75 1
    public function getName()
76
    {
77 1
        return $this->name;
78
    }
79
80
    /**
81
     * getName
82
     *
83
     * @return string
84
     */
85
    public function getTitle()
86
    {
87
        return $this->translate($this->tableConfiguration['ctrl']['title']);
88
    }
89
90
    /**
91
     * getColumns
92
     *
93
     * @return array
94
     */
95 8
    public function getColumns()
96
    {
97 8
        return array_keys($this->tableConfiguration['columns']);
98
    }
99
100
    /**
101
     * getColumnConfiguration
102
     *
103
     * @param string $columnName
104
     * @return array
105
     */
106 3
    public function getColumnConfiguration($columnName)
107
    {
108 3
        if (!is_string($columnName)) {
109 1
            throw new \InvalidArgumentException(
110 1
                'Columns must be type of string. Type of ' . gettype($columnName) . ' given.'
111
            );
112
        }
113 2
        if (!isset($this->tableConfiguration['columns'][$columnName])) {
114 1
            throw new \InvalidArgumentException(
115 1
                'Column ' . $columnName . ' does not exist for table ' . $this->name
116
            );
117
        }
118
119 1
        return array($columnName => $this->tableConfiguration['columns'][$columnName]['config']);
120
    }
121
122
    /**
123
     * @return array
124
     */
125 1
    public static function getAllTables()
126
    {
127 1
        $tables = array();
128 1
        foreach (array_keys($GLOBALS['TCA']) as $tableName) {
129 1
            $tables[$tableName] = $tableName;
130
        }
131 1
        return $tables;
132
    }
133
}
134