Conditions | 14 |
Paths | 738 |
Total Lines | 108 |
Code Lines | 57 |
Lines | 7 |
Ratio | 6.48 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | protected function execute(InputInterface $input, OutputInterface $output) |
||
45 | { |
||
46 | /** @var \Eccube\Application $app */ |
||
47 | $app = $this->getSilexApplication(); |
||
48 | |||
49 | // // プロキシのクリア |
||
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) { |
|
81 | $rc = new \ReflectionClass($className); |
||
82 | $sourceFile = $rc->getFileName(); |
||
83 | if (in_array($sourceFile, $includedFiles)) { |
||
84 | $traits[] = $className; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // traitから@EntityExtensionを抽出 |
||
89 | $reader = new AnnotationReader(); |
||
90 | $proxies = []; |
||
91 | foreach ($traits as $trait) { |
||
92 | $anno = $reader->getClassAnnotation(new \ReflectionClass($trait), EntityExtension::class); |
||
93 | if ($anno) { |
||
94 | $proxies[$anno->value][] = $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;が存在せず, |
||
114 | // traitに定義されているフィールド,メソッドがクラス側に追加されてしまう |
||
115 | if ($generator->hasProperty($prop->getName())) { |
||
116 | // $generator->removeProperty()はzend-code 2.6.3 では未実装なのでリフレクションで削除. |
||
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())) { |
||
127 | $generator->removeMethod($method->getName()); |
||
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/proxy/entity'; |
||
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 |