Conditions | 11 |
Paths | 49 |
Total Lines | 82 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
61 | public function generate(BundleInterface $bundle, $entityDocument, $metadata, $columns = false) |
||
62 | { |
||
63 | if ($metadata instanceof ClassMetadataInfo) { |
||
64 | $manager = '@doctrine.orm.default_entity_manager'; |
||
65 | $class = 'Dtc\GridBundle\Grid\Source\EntityGridSource'; |
||
66 | } elseif ($metadata instanceof \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo) { |
||
67 | $manager = '@doctrine_mongodb.odm.default_document_manager'; |
||
68 | $class = 'Dtc\GridBundle\Grid\Source\DocumentGridSource'; |
||
69 | } else { |
||
70 | throw new \Exception(__METHOD__.' - Unknown class for metadata: '.get_class($metadata)); |
||
71 | } |
||
72 | $entityDocumentClassPath = $metadata->getReflectionClass()->getName(); |
||
73 | |||
74 | $parts = explode('\\', $entityDocument); |
||
75 | $entityDocumentClass = array_pop($parts); |
||
76 | $files = []; |
||
77 | |||
78 | if ($columns) { |
||
79 | list($gridColumnClass, $gridColumnsNamespace, $gridColumnPath, $templatePath) = |
||
80 | $this->generateColumns($bundle, $entityDocument, $metadata); |
||
81 | |||
82 | $files = array( |
||
83 | 'grid_columns' => $gridColumnPath, |
||
84 | 'grid_template' => $templatePath, |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | // Check to see if the files exists |
||
89 | if ($this->saveCache) { |
||
90 | foreach ($this->saveCache as $file => $output) { |
||
91 | $this->saveFile($file, $output); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $config = array(); |
||
96 | $serviceName = 'grid.source.'.strtolower($entityDocumentClass); |
||
97 | $config[$serviceName] = array( |
||
98 | 'class' => $class, |
||
99 | 'arguments' => array($manager, $entityDocumentClassPath), |
||
100 | 'tags' => array(array('name' => 'dtc_grid.source')), |
||
101 | 'calls' => array( |
||
102 | array('autoDiscoverColumns'), |
||
103 | ), ); |
||
104 | |||
105 | if ($columns && isset($gridColumnsNamespace) && isset($gridColumnClass)) { |
||
106 | $config[$serviceName]['calls'] = array( |
||
107 | array('setColumns', array( |
||
108 | '@'.$serviceName.'.columns', |
||
109 | ), |
||
110 | ), |
||
111 | ); |
||
112 | |||
113 | $config[$serviceName.'.columns'] = array( |
||
114 | 'class' => $gridColumnsNamespace.'\\'.$gridColumnClass, |
||
115 | 'arguments' => array('@twig'), |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $configFile = $bundle->getPath().'/Resources/config/grid.yml'; |
||
120 | $services = array(); |
||
121 | if (file_exists($configFile)) { |
||
122 | $services = Yaml::parse($contents = file_get_contents($configFile)); |
||
123 | if (isset($services['services'])) { |
||
124 | $services['services'] = array_merge($services['services'], $config); |
||
125 | } else { |
||
126 | $services['services'] = $config; |
||
127 | } |
||
128 | } else { |
||
129 | $services['services'] = $config; |
||
130 | } |
||
131 | |||
132 | $this->saveFile($configFile, Yaml::dump($services, 3)); |
||
133 | |||
134 | $params = array( |
||
135 | 'gridsource_id' => $serviceName, |
||
136 | 'files' => $files, |
||
137 | ); |
||
138 | |||
139 | $output = $this->render($this->skeletonDir, 'controller.php.twig', $params); |
||
140 | echo $output; |
||
141 | exit(); |
||
142 | } |
||
143 | |||
180 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: