Completed
Push — master ( 439406...4b2726 )
by Hong
03:09
created

FromTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 0
dl 5
loc 75
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 14 3
A table() 0 6 1
A multipleFrom() 0 10 3
A buildFrom() 5 11 2
quoteAlias() 0 1 ?
quoteItem() 0 1 ?
getClause() 0 1 ?
joinClause() 0 6 ?

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
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\Clause\FromInterface;
18
19
/**
20
 * FromTrait
21
 *
22
 * Implementation of FromInterface
23
 *
24
 * @package Phossa2\Query
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     FromInterface
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
trait FromTrait
31
{
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function from($table, /*# string */ $alias = '')
36
    {
37
        if (is_array($table)) {
38
            $this->multipleFrom($table);
39
        } else {
40
            $clause = &$this->getClause('FROM');
41
            if (empty($alias)) {
42
                $clause[] = $table;
43
            } else {
44
                $clause[(string) $alias] = $table;
45
            }
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function table($table, /*# string */ $alias = '')
54
    {
55
        $clause = &$this->getClause('FROM');
56
        $clause = [];
57
        return $this->from($table, $alias);
58
    }
59
60
    /**
61
     * from multiple tables
62
     *
63
     * @param  array $tables
64
     * @access protected
65
     */
66
    protected function multipleFrom(array $tables)
67
    {
68
        foreach ($tables as $key => $val) {
69
            if (is_int($key)) {
70
                $this->from($val);
71
            } else {
72
                $this->from($key, $val);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Build FROM
79
     *
80
     * @return array
81
     * @access protected
82
     */
83
    protected function buildFrom(array $settings)/*# : string */
84
    {
85
        $result = [];
86
        $clause = &$this->getClause('FROM');
87 View Code Duplication
        foreach ($clause as $as => $tbl) {
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...
88
            $alias = $this->quoteAlias($as);
89
            $table = $this->quoteItem($tbl);
90
            $result[] = $table . $alias;
91
        }
92
        return $this->joinClause('FROM', ',', $result, $settings);
93
    }
94
95
    abstract protected function quoteAlias($alias)/*# : string */;
96
    abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */;
97
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
98
    abstract protected function joinClause(
99
        /*# : string */ $prefix,
100
        /*# : string */ $seperator,
101
        array $clause,
102
        array $settings
103
    )/*# : string */;
104
}
105