Failed Conditions
Pull Request — experimental/3.1 (#2268)
by chihiro
68:29 queued 15:11
created

GenProxyCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 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\Command;
25
26
use Doctrine\Common\Annotations\AnnotationReader;
27
use Eccube\Annotation\EntityExt;
28
use Knp\Command\Command;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\Filesystem\Filesystem;
32
use Symfony\CS\Finder;
33
34
35
class GenProxyCommand extends Command
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('debug:entity-proxy')
41
            ->setDescription('generate entity proxies');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        /** @var \Eccube\Application $app */
47
        $app = $this->getSilexApplication();
48
49
//        // プロキシのクリア
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
//        $files = Finder::create()
51
//            ->in($app['config']['root_dir'].'/app/cache/doctrine/entity-proxies')
52
//            ->name('*.php')
53
//            ->files();
54
//        $fs = new Filesystem();
55
//        foreach ($files as $file) {
56
//            $output->writeln('remove -> '.$file->getRealPath());
57
//            $fs->remove($file->getRealPath());
58
//        }
59
60
        // Acmeからファイルを抽出
61
        $files = Finder::create()
62
            ->in(
63
                [
64
                    $app['config']['root_dir'].'/app/Acme/Entity',
65
                ]
66
            )
67
            ->name('*.php')
68
            ->files();
69
70
        // traitの一覧を取得
71
        $traits = [];
72
        $includedFiles = [];
73
        foreach ($files as $file) {
74
            require_once $file->getRealPath();
75
            $includedFiles[] = $file->getRealPath();
76
        }
77
78
        $declared = get_declared_traits();
79
80 View Code Duplication
        foreach ($declared as $className) {
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...
81
            $rc = new \ReflectionClass($className);
82
            $sourceFile = $rc->getFileName();
83
            if (in_array($sourceFile, $includedFiles)) {
84
                $traits[] = $className;
85
            }
86
        }
87
88
        // traitから@EntityExtを抽出
89
        $reader = new AnnotationReader();
90
        $proxies = [];
91
        foreach ($traits as $trait) {
92
            $anno = $reader->getClassAnnotation(new \ReflectionClass($trait), EntityExt::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $anno is correct as $reader->getClassAnnotat...ation\EntityExt::class) (which targets Doctrine\Common\Annotati...r::getClassAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
            if ($anno) {
94
                $proxies[$anno->target][] = $trait;
95
            }
96
        }
97
        // プロキシファイルの生成
98
        foreach ($proxies as $targetEntity => $traits) {
99
            $rc = new \Zend\Code\Reflection\ClassReflection($targetEntity);
100
            $generator
101
                = \Zend\Code\Generator\ClassGenerator::fromReflection($rc);
102
103
            $uses = \Zend\Code\Generator\FileGenerator::fromReflectedFileName($rc->getFileName())
104
                ->getUses();
105
106
            foreach ($uses as $use) {
107
                $generator->addUse($use[0], $use[1]);
108
            }
109
110
            foreach ($traits as $trait) {
111
                $rt = new \Zend\Code\Reflection\ClassReflection($trait);
112
                foreach ($rt->getProperties() as $prop) {
113
                    // すでにProxyがある場合, $generatorにuse XxxTrait;が存在せず,
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
                    // traitに定義されているフィールド,メソッドがクラス側に追加されてしまう
115
                    if ($generator->hasProperty($prop->getName())) {
116
                        // $generator->removeProperty()はzend-code 2.6.3 では未実装なのでリフレクションで削除.
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
                        $generatorRefObj = new \ReflectionObject($generator);
118
                        $generatorRefProp = $generatorRefObj->getProperty('properties');
119
                        $generatorRefProp->setAccessible(true);
120
                        $properies = $generatorRefProp->getValue($generator);
121
                        unset($properies[$prop->getName()]);
122
                        $generatorRefProp->setValue($generator, $properies);
123
                    }
124
                }
125
                foreach ($rt->getMethods() as $method) {
126
                    if ($generator->hasMethod($method->getName())) {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
127
                        $generator->removeMethod($method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
128
                    }
129
                }
130
                $generator->addTrait('\\'.$trait);
131
            }
132
133
            // extendしたクラスが相対パスになるので
134
            $extendClass = $generator->getExtendedClass();
135
            $generator->setExtendedClass('\\'.$extendClass);
136
137
            // interfaceが相対パスになるので
138
            $interfaces = $generator->getImplementedInterfaces();
139
            foreach ($interfaces as &$interface) {
140
                $interface = '\\'.$interface;
141
            }
142
            $generator->setImplementedInterfaces($interfaces);
143
144
            $dir = $app['config']['root_dir'].'/app/cache/doctrine/entity-proxies';
145
            $file = basename($rc->getFileName());
146
147
            $code = $generator->generate();
148
            file_put_contents($dir.'/'.$file, '<?php '.PHP_EOL.$code);
149
            $output->writeln('gen -> '.$dir.'/'.$file);
150
        }
151
    }
152
}
153