| Total Complexity | 41 |
| Total Lines | 195 |
| Duplicated Lines | 3.59 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 40 | class NamespaceIndexBuilder extends PhpDomainBuilder { |
||
| 41 | |||
| 42 | const RENDER_INDEX_NAMESPACE = 0; |
||
| 43 | const RENDER_INDEX_CLASSES = 1; |
||
| 44 | const RENDER_INDEX_TRAITS = 2; |
||
| 45 | const RENDER_INDEX_INTERFACES = 3; |
||
| 46 | const RENDER_INDEX_FUNCTIONS = 4; |
||
| 47 | const RENDER_INDEX_CONSTANTS = 5; |
||
| 48 | |||
| 49 | /** @var Namespace_ */ |
||
| 50 | private $currentNamespace; |
||
| 51 | |||
| 52 | /** @var Namespace_[] */ |
||
| 53 | private $namespaces; |
||
| 54 | |||
| 55 | /** @var Namespace_[] */ |
||
| 56 | private $childNamespaces = []; |
||
| 57 | |||
| 58 | /** @var Function_[] */ |
||
| 59 | private $functions; |
||
| 60 | |||
| 61 | /** @var Constant[] */ |
||
| 62 | private $constants; |
||
| 63 | |||
| 64 | public function __construct($extensions, $namespaces, Namespace_ $current, $functions, $constants) { |
||
| 65 | parent::__construct($extensions); |
||
| 66 | $this->namespaces = $namespaces; |
||
| 67 | $this->currentNamespace = $current; |
||
| 68 | $this->functions = $functions; |
||
| 69 | $this->constants = $constants; |
||
| 70 | $this->findChildNamespaces(); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Find child namespaces for current namespace |
||
| 75 | */ |
||
| 76 | private function findChildNamespaces() { |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | public function render() { |
||
| 95 | $currentNamespaceFqsen = (string)$this->currentNamespace->getFqsen(); |
||
| 96 | if ($currentNamespaceFqsen !== '\\') { |
||
| 97 | $label = str_replace('\\', '-', $currentNamespaceFqsen); |
||
| 98 | $this->addLine('.. _namespace' . $label . ':')->addLine(); |
||
| 99 | $this->addH1(RstBuilder::escape($this->currentNamespace->getName())); |
||
| 100 | $this->addLine(self::escape($currentNamespaceFqsen))->addLine(); |
||
| 101 | } else { |
||
| 102 | $label = 'root-namespace'; |
||
| 103 | $this->addLine('.. _namespace-' . $label . ':')->addLine(); |
||
| 104 | $this->addH1(RstBuilder::escape('\\')); |
||
| 105 | } |
||
| 106 | $this->addLine(); |
||
| 107 | |||
| 108 | $this->addIndex(self::RENDER_INDEX_NAMESPACE); |
||
| 109 | $this->addIndex(self::RENDER_INDEX_INTERFACES); |
||
| 110 | $this->addIndex(self::RENDER_INDEX_CLASSES); |
||
| 111 | $this->addIndex(self::RENDER_INDEX_TRAITS); |
||
| 112 | |||
| 113 | if ($this->shouldRenderIndex(self::RENDER_INDEX_CONSTANTS)) { |
||
| 114 | $this->addConstants($this->constants); |
||
| 115 | } |
||
| 116 | $this->addFunctions(); |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function addIndex($type) { |
||
| 120 | if ($this->shouldRenderIndex($type)) { |
||
| 121 | $this->addH2($this->getHeaderForType($type)); |
||
| 122 | $this->addLine('.. toctree::'); |
||
| 123 | $this->indent(); |
||
| 124 | $this->addLine(':maxdepth: 1')->addLine(); |
||
| 125 | /** @var Fqsen $entry */ |
||
| 126 | foreach ($this->getElementList($type) as $entry) { |
||
| 127 | if (!$this->shouldRenderIndex($type, $entry)) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | if ($type === self::RENDER_INDEX_NAMESPACE) { |
||
| 131 | $this->addLine($entry->getName() . ' <' . $entry->getName() . '/index>'); |
||
| 132 | } else { |
||
| 133 | $this->addElementTocEntry($entry); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | $this->unindent(); |
||
| 137 | $this->addLine(); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | private function addFunctions() { |
||
| 142 | if (!$this->shouldRenderIndex(self::RENDER_INDEX_FUNCTIONS)) { |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | $this->addH2('Functions'); |
||
| 146 | /** @var Function_ $function */ |
||
| 147 | foreach ($this->functions as $function) { |
||
| 148 | if (!$this->shouldRenderIndex(self::RENDER_INDEX_FUNCTIONS, $function)) { |
||
| 149 | continue; |
||
| 150 | } |
||
| 151 | $docBlock = $function->getDocBlock(); |
||
| 152 | $params = []; |
||
| 153 | if ($docBlock !== null) { |
||
| 154 | /** @var Param $param */ |
||
| 155 | foreach ($docBlock->getTagsByName('param') as $param) { |
||
| 156 | $params[$param->getVariableName()] = $param; |
||
|
1 ignored issue
–
show
|
|||
| 157 | } |
||
| 158 | } |
||
| 159 | $args = ''; |
||
| 160 | /** @var Argument $argument */ |
||
| 161 | foreach ($function->getArguments() as $argument) { |
||
| 162 | // TODO: defaults, types |
||
| 163 | $args .= '$' . $argument->getName() . ', '; |
||
| 164 | } |
||
| 165 | $args = substr($args, 0, -2); |
||
| 166 | $this->beginPhpDomain('function', $function->getName() . '(' . $args . ')'); |
||
| 167 | $this->addDocBlockDescription($function); |
||
| 168 | View Code Duplication | if (!empty($params)) { |
|
| 169 | foreach ($function->getArguments() as $argument) { |
||
| 170 | /** @var Param $param */ |
||
| 171 | $param = $params[$argument->getName()]; |
||
| 172 | if ($param !== null) { |
||
| 173 | $this->addMultiline(':param ' . self::escape($param->getType()) . ' $' . $argument->getName() . ': ' . $param->getDescription(), true); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | $this->endPhpDomain('function'); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | private function addElementTocEntry(Fqsen $entry) { |
||
| 182 | $currentNamespaceFqsen = (string)$this->currentNamespace->getFqsen(); |
||
| 183 | $subPath = $entry; |
||
| 184 | if ($currentNamespaceFqsen !== '\\' && substr($entry, 0, strlen($currentNamespaceFqsen)) === $currentNamespaceFqsen) { |
||
| 185 | $subPath = substr($entry, strlen($currentNamespaceFqsen)); |
||
| 186 | } |
||
| 187 | $path = substr(str_replace('\\', '/', $subPath), 1); |
||
| 188 | $this->addLine($entry->getName() . ' <' . $path . '>'); |
||
| 189 | } |
||
| 190 | |||
| 191 | private function shouldRenderIndex($type, $element = null) { |
||
| 192 | foreach ($this->extensions as $extension) { |
||
| 193 | if (!$extension->shouldRenderIndex($type, $element)) { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | if ($element === null) { |
||
| 198 | return (count($this->getElementList($type)) > 0); |
||
| 199 | } |
||
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | private function getHeaderForType($type) { |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param int $type |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | private function getElementList($type) { |
||
| 235 | } |
||
| 236 | |||
| 237 | } |