| Total Complexity | 42 |
| Total Lines | 231 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Complex classes like NamespaceIndexBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NamespaceIndexBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class NamespaceIndexBuilder extends PhpDomainBuilder |
||
| 35 | { |
||
| 36 | const RENDER_INDEX_NAMESPACE = 0; |
||
| 37 | const RENDER_INDEX_CLASSES = 1; |
||
| 38 | const RENDER_INDEX_TRAITS = 2; |
||
| 39 | const RENDER_INDEX_INTERFACES = 3; |
||
| 40 | const RENDER_INDEX_FUNCTIONS = 4; |
||
| 41 | const RENDER_INDEX_CONSTANTS = 5; |
||
| 42 | |||
| 43 | /** @var Namespace_ */ |
||
| 44 | private $currentNamespace; |
||
| 45 | |||
| 46 | /** @var Namespace_[] */ |
||
| 47 | private $namespaces; |
||
| 48 | |||
| 49 | /** @var Namespace_[] */ |
||
| 50 | private $childNamespaces = []; |
||
| 51 | |||
| 52 | /** @var Function_[] */ |
||
| 53 | private $functions; |
||
| 54 | |||
| 55 | /** @var Constant[] */ |
||
| 56 | private $constants; |
||
| 57 | |||
| 58 | public function __construct($extensions, $namespaces, Namespace_ $current, $functions, $constants) |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Find child namespaces for current namespace. |
||
| 70 | */ |
||
| 71 | private function findChildNamespaces() |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | public function render() |
||
| 100 | { |
||
| 101 | $currentNamespaceFqsen = (string) $this->currentNamespace->getFqsen(); |
||
| 102 | if ($currentNamespaceFqsen !== '\\') { |
||
| 103 | $label = str_replace('\\', '-', $currentNamespaceFqsen); |
||
| 104 | $this->addLine('.. _namespace'.$label.':')->addLine(); |
||
| 105 | $this->addH1(RstBuilder::escape($this->currentNamespace->getName())); |
||
| 106 | $this->addLine(self::escape($currentNamespaceFqsen))->addLine(); |
||
| 107 | } else { |
||
| 108 | $label = 'root-namespace'; |
||
| 109 | $this->addLine('.. _namespace-'.$label.':')->addLine(); |
||
| 110 | $this->addH1(RstBuilder::escape('\\')); |
||
| 111 | } |
||
| 112 | $this->addLine(); |
||
| 113 | |||
| 114 | $this->addIndex(self::RENDER_INDEX_NAMESPACE); |
||
| 115 | $this->addIndex(self::RENDER_INDEX_INTERFACES); |
||
| 116 | $this->addIndex(self::RENDER_INDEX_CLASSES); |
||
| 117 | $this->addIndex(self::RENDER_INDEX_TRAITS); |
||
| 118 | |||
| 119 | if ($this->shouldRenderIndex(self::RENDER_INDEX_CONSTANTS)) { |
||
| 120 | $this->addConstants($this->constants); |
||
| 121 | } |
||
| 122 | $this->addFunctions(); |
||
| 123 | } |
||
| 124 | |||
| 125 | protected function addIndex($type) |
||
| 126 | { |
||
| 127 | if ($this->shouldRenderIndex($type)) { |
||
| 128 | $this->addH2($this->getHeaderForType($type)); |
||
| 129 | $this->addLine('.. toctree::'); |
||
| 130 | $this->indent(); |
||
| 131 | $this->addLine(':maxdepth: 1')->addLine(); |
||
| 132 | /** @var Fqsen $entry */ |
||
| 133 | foreach ($this->getElementList($type) as $entry) { |
||
| 134 | if (!$this->shouldRenderIndex($type, $entry)) { |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | if ($type === self::RENDER_INDEX_NAMESPACE) { |
||
| 138 | $this->addLine($entry->getName().' <'.$entry->getName().'/index>'); |
||
| 139 | } else { |
||
| 140 | $this->addElementTocEntry($entry); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | $this->unindent(); |
||
| 144 | $this->addLine(); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | private function shouldRenderIndex($type, $element = null) |
||
| 149 | { |
||
| 150 | foreach ($this->extensions as $extension) { |
||
| 151 | if (!$extension->shouldRenderIndex($type, $element)) { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | if ($element === null) { |
||
| 156 | return count($this->getElementList($type)) > 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param int $type |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | private function getElementList($type) |
||
| 168 | { |
||
| 169 | $elements = []; |
||
| 170 | switch ($type) { |
||
| 171 | case self::RENDER_INDEX_NAMESPACE: |
||
| 172 | $elements = $this->childNamespaces; |
||
| 173 | break; |
||
| 174 | case self::RENDER_INDEX_CLASSES: |
||
| 175 | $elements = $this->currentNamespace->getClasses(); |
||
| 176 | break; |
||
| 177 | case self::RENDER_INDEX_INTERFACES: |
||
| 178 | $elements = $this->currentNamespace->getInterfaces(); |
||
| 179 | break; |
||
| 180 | case self::RENDER_INDEX_TRAITS: |
||
| 181 | $elements = $this->currentNamespace->getTraits(); |
||
| 182 | break; |
||
| 183 | case self::RENDER_INDEX_FUNCTIONS: |
||
| 184 | $elements = $this->functions; |
||
| 185 | break; |
||
| 186 | case self::RENDER_INDEX_CONSTANTS: |
||
| 187 | $elements = $this->constants; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $elements; |
||
| 192 | } |
||
| 193 | |||
| 194 | private function getHeaderForType($type) |
||
| 205 | } |
||
| 206 | |||
| 207 | private function addElementTocEntry(Fqsen $entry) |
||
| 208 | { |
||
| 220 | } |
||
| 221 | |||
| 222 | private function addFunctions() |
||
| 268 |