Failed Conditions
Push — master ( ee6ce9...9ed6ac )
by Yangsin
485:10 queued 475:23
created

Extract::parse()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 6
Ratio 21.43 %

Code Coverage

Tests 14
CRAP Score 9.9357

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 6
loc 28
ccs 14
cts 23
cp 0.6087
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 19
nc 6
nop 1
crap 9.9357
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2016 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Doctrine\ORM\Query;
25
26
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
27
use Doctrine\ORM\Query\Lexer;
28
use Doctrine\ORM\Query\Parser;
29
use Doctrine\ORM\Query\SqlWalker;
30
31
/**
32
 * EXTRACT (field FROM [type] source)
33
 *  field:
34
 *      単位指定子
35
 *          - YEAR
36
 *          - MONTH
37
 *          - DAY
38
 *          - HOUR
39
 *          - MINUTE
40
 *          - SECOND
41
 *          - WEEK
42
 *  type:
43
 *      日付/時刻データ型
44
 *      sourceが文字列の場合のみ必須
45
 *          - TIMESTAMP
46
 *          - DATE
47
 *          - TIME
48
 *  source:
49
 *      日付/時刻データ型、もしくは日付/時刻を表す文字列
50
 *      文字列の場合はtypeが必須
51
 *
52
 * @package Eccube\Doctrine\ORM\Query
53
 */
54
class Extract extends FunctionNode
55
{
56
    protected $field;
57
    protected $type;
58
    protected $source;
59
60
    protected $formats = array(
61
        'YEAR'      => '%Y',
62
        'MONTH'     => '%m',
63
        'DAY'       => '%d',
64
        'HOUR'      => '%H',
65
        'MINUTE'    => '%M',
66
        'SECOND'    => '%S',
67
        'WEEK'      => '%W',
68
    );
69
70
    protected $dateTimeTypes = array(
71
        'TIMESTAMP',
72
        'DATE',
73
        'TIME',
74
    );
75
76 1
    public function parse(Parser $parser)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
77
    {
78 1
        $lexer = $parser->getLexer();
79 1
        $parser->match(Lexer::T_IDENTIFIER);
80 1
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
81
82 1
        $upperField = strtoupper($lexer->lookahead['value']);
83 1 View Code Duplication
        if ($lexer->lookahead['type'] !== Lexer::T_IDENTIFIER || !isset($this->formats[$upperField])) {
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...
84
            $parser->syntaxError(implode('/', array_keys($this->formats)));
85
        }
86
87 1
        $parser->match(Lexer::T_IDENTIFIER);
88 1
        $this->field = $upperField;
89 1
        $parser->match(Lexer::T_FROM);
90
91 1
        $next = $lexer->glimpse();
92 1
        if (isset($next['type']) && $next['type'] === Lexer::T_STRING) {
93
            $upperType = strtoupper($lexer->lookahead['value']);
94 View Code Duplication
            if ($lexer->lookahead['type'] !== Lexer::T_IDENTIFIER || !in_array($upperType, $this->dateTimeTypes, true)) {
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...
95
                $parser->syntaxError(implode('/', $this->dateTimeTypes));
96
            }
97
            $parser->match(Lexer::T_IDENTIFIER);
98
            $this->type = $upperType;
99
        }
100
101 1
        $this->source = $parser->ArithmeticPrimary();
102 1
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
103 1
    }
104
105 1
    public function getSql(SqlWalker $sqlWalker)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
106
    {
107 1
        $driver = $sqlWalker->getConnection()->getDriver()->getName();
108 1
        if ($driver == 'pdo_sqlite') {
109
            return sprintf("CAST(STRFTIME('%s', %s) AS INTEGER)", $this->formats[$this->field], $this->source->dispatch($sqlWalker));
110
        } else {
111 1
            return sprintf('EXTRACT(%s FROM %s %s)', $this->field, (string)$this->type, $this->source->dispatch($sqlWalker));
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
112
        }
113
    }
114
}