| Conditions | 11 |
| Paths | 25 |
| Total Lines | 65 |
| Code Lines | 30 |
| 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 |
||
| 73 | public function create(AnnotatedInterface $model) |
||
| 74 | { |
||
| 75 | $className = get_class($model); |
||
| 76 | |||
| 77 | // If have or don't have indexes skip further checks |
||
| 78 | if(array_key_exists($className, self::$haveIndex)) |
||
| 79 | { |
||
| 80 | return self::$haveIndex[$className]; |
||
| 81 | } |
||
| 82 | |||
| 83 | $fieldMetas = ManganMeta::create($model)->fields(); |
||
| 84 | |||
| 85 | // Filter out fields without index |
||
| 86 | foreach($fieldMetas as $key => $metaProperty) |
||
| 87 | { |
||
| 88 | if(empty($metaProperty->index)) |
||
| 89 | { |
||
| 90 | unset($fieldMetas[$key]); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Does not have indexes, mark as index-less |
||
| 95 | if(empty($fieldMetas)) |
||
| 96 | { |
||
| 97 | self::$haveIndex[$className] = false; |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | $path = $this->getStoragePath($model, $className); |
||
| 102 | |||
| 103 | $data = SoftIncluder::includeFile($path); |
||
| 104 | |||
| 105 | if(!empty($data)) |
||
| 106 | { |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | $results = []; |
||
| 110 | $indexes = []; |
||
| 111 | foreach($fieldMetas as $fieldMeta) |
||
| 112 | { |
||
| 113 | if(empty($fieldMeta->index)) |
||
| 114 | { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | /* @var $fieldMeta DocumentPropertyMeta */ |
||
| 118 | |||
| 119 | foreach($fieldMeta->index as $indexMeta) |
||
| 120 | { |
||
| 121 | $index = new IndexModel($model, $indexMeta); |
||
| 122 | $results[] = (int)$index->apply(); |
||
| 123 | $indexes[] = $index->getIndexes(); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | self::$haveIndex[$className] = true; |
||
| 127 | |||
| 128 | $dir = dirname($path); |
||
| 129 | if(!self::$haveDir && !file_exists($dir)) |
||
| 130 | { |
||
| 131 | self::$haveDir = mkdir($dir); |
||
| 132 | } |
||
| 133 | |||
| 134 | file_put_contents($path, PhpExporter::export($indexes, 'Auto generated, do not modify')); |
||
| 135 | |||
| 136 | return array_sum($results) === count($results); |
||
| 137 | } |
||
| 138 | |||
| 158 | } |