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:
| 1 | <?php |
||
| 21 | class AttributeDecorator implements AttributeDecoratorInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Server. |
||
| 25 | * |
||
| 26 | * @var Server |
||
| 27 | */ |
||
| 28 | protected $server; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Filesystem. |
||
| 32 | * |
||
| 33 | * @var Filesystem |
||
| 34 | */ |
||
| 35 | protected $fs; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Acl. |
||
| 39 | * |
||
| 40 | * @var Acl |
||
| 41 | */ |
||
| 42 | protected $acl; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Role decorator. |
||
| 46 | * |
||
| 47 | * @var RoleAttributeDecorator |
||
| 48 | */ |
||
| 49 | protected $role_decorator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Custom attributes. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $custom = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Init. |
||
| 60 | */ |
||
| 61 | 6 | public function __construct(Server $server, Acl $acl, RoleAttributeDecorator $role_decorator) |
|
| 62 | { |
||
| 63 | 6 | $this->server = $server; |
|
| 64 | 6 | $this->acl = $acl; |
|
| 65 | 6 | $this->role_decorator = $role_decorator; |
|
| 66 | 6 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Decorate attributes. |
||
| 70 | */ |
||
| 71 | 6 | public function decorate(NodeInterface $node, array $attributes = []): array |
|
| 72 | { |
||
| 73 | 6 | $requested = $attributes; |
|
| 74 | 6 | $attributes = $node->getAttributes(); |
|
| 75 | |||
| 76 | 6 | $attrs = array_merge( |
|
| 77 | 6 | $this->getAttributes($node, $attributes), |
|
| 78 | 6 | $this->getTimeAttributes($node, $attributes), |
|
| 79 | 6 | $this->getTypeAttributes($node, $attributes), |
|
| 80 | 6 | $this->custom |
|
| 81 | ); |
||
| 82 | |||
| 83 | 6 | if (0 === count($requested)) { |
|
| 84 | 2 | return $this->translateAttributes($node, $attrs); |
|
| 85 | } |
||
| 86 | |||
| 87 | 4 | return $this->translateAttributes($node, array_intersect_key($attrs, array_flip($requested))); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Add decorator. |
||
| 92 | */ |
||
| 93 | 2 | public function addDecorator(string $attribute, Closure $decorator): self |
|
| 94 | { |
||
| 95 | 2 | $this->custom[$attribute] = $decorator; |
|
| 96 | |||
| 97 | 2 | return $this; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get Attributes. |
||
| 102 | */ |
||
| 103 | 6 | protected function getAttributes(NodeInterface $node, array $attributes): array |
|
| 104 | { |
||
| 105 | 6 | $acl = $this->acl; |
|
| 106 | 6 | $server = $this->server; |
|
| 107 | 6 | $fs = $this->server->getFilesystem(); |
|
| 108 | 6 | $decorator = $this->role_decorator; |
|
| 109 | |||
| 110 | return [ |
||
| 111 | 6 | 'id' => (string) $attributes['_id'], |
|
| 112 | 6 | 'name' => (string) $attributes['name'], |
|
| 113 | 6 | 'mime' => (string) $attributes['mime'], |
|
| 114 | 6 | 'readonly' => (bool) $attributes['readonly'], |
|
| 115 | 6 | 'directory' => $node instanceof Collection, |
|
| 116 | 6 | 'meta' => function ($node) { |
|
| 117 | 2 | return (object) $node->getMetaAttributes(); |
|
| 118 | 6 | }, |
|
| 119 | 6 | 'size' => function ($node) { |
|
| 120 | 2 | return $node->getSize(); |
|
| 121 | 6 | }, |
|
| 122 | 6 | 'path' => function ($node) { |
|
| 123 | try { |
||
| 124 | 2 | return $node->getPath(); |
|
| 125 | } catch (\Exception $e) { |
||
| 126 | return null; |
||
| 127 | } |
||
| 128 | 6 | }, |
|
| 129 | 6 | 'parent' => function ($node) { |
|
| 130 | 2 | $parent = $node->getParent(); |
|
| 131 | |||
| 132 | 2 | if (null === $parent || $parent->isRoot()) { |
|
| 133 | 2 | return null; |
|
| 134 | } |
||
| 135 | |||
| 136 | return $this->decorate($node->getParent(), ['id', 'name', '_links']); |
||
| 137 | 6 | }, |
|
| 138 | 6 | 'access' => function ($node) use ($acl) { |
|
| 139 | 2 | return $acl->getAclPrivilege($node); |
|
| 140 | 6 | }, |
|
| 141 | 6 | 'acl' => function ($node) use ($attributes) { |
|
| 142 | 2 | if ($node->isShareMember() && count($attributes['acl']) > 0) { |
|
| 143 | return $node->getAcl(); |
||
| 144 | } |
||
| 145 | |||
| 146 | 2 | return null; |
|
| 147 | 6 | }, |
|
| 148 | 6 | 'share' => function ($node) use ($fs) { |
|
| 149 | 2 | if ($node->isShared() || !$node->isSpecial()) { |
|
| 150 | 2 | return null; |
|
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | return $this->decorate($fs->findNodeById($node->getShareId(true)), ['id', 'name', '_links']); |
||
| 155 | } catch (\Exception $e) { |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | 6 | }, |
|
| 159 | 6 | 'sharename' => function ($node) { |
|
| 160 | 2 | if (!$node->isShared()) { |
|
| 161 | 2 | return null; |
|
| 162 | } |
||
| 163 | |||
| 164 | try { |
||
| 165 | return $node->getShareName(); |
||
| 166 | } catch (\Exception $e) { |
||
| 167 | return null; |
||
| 168 | } |
||
| 169 | 6 | }, |
|
| 170 | 6 | 'shareowner' => function ($node) use ($server, $fs, $decorator) { |
|
| 171 | 2 | if (!$node->isSpecial()) { |
|
| 172 | 2 | return null; |
|
| 173 | } |
||
| 174 | |||
| 175 | try { |
||
| 176 | return $decorator->decorate( |
||
| 177 | $server->getUserById($fs->findRawNode($node->getShareId())['owner']), |
||
| 178 | ['id', 'name', '_links'] |
||
| 179 | ); |
||
| 180 | } catch (\Exception $e) { |
||
| 181 | return null; |
||
| 182 | } |
||
| 183 | 6 | }, |
|
| 184 | 6 | 'owner' => function ($node) use ($server, $decorator) { |
|
| 185 | try { |
||
| 186 | 2 | return $decorator->decorate( |
|
| 187 | 2 | $server->getUserById($node->getOwner()), |
|
| 188 | ['id', 'name', '_links'] |
||
| 189 | ); |
||
| 190 | 2 | } catch (\Exception $e) { |
|
| 191 | 2 | return null; |
|
| 192 | } |
||
| 193 | 6 | }, |
|
| 194 | 6 | 'external_storage' => function ($node) use ($fs, $attributes) { |
|
| 195 | 2 | if ($node->getMount() === null || $node instanceof Collection && $node->isMounted()) { |
|
| 196 | 2 | return null; |
|
| 197 | } |
||
| 198 | |||
| 199 | return $this->decorate($fs->findNodeById($attributes['storage_reference']), ['id', 'name', '_links']); |
||
| 200 | 6 | }, |
|
| 201 | ]; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get Attributes. |
||
| 206 | * |
||
| 207 | * @param NodeInterface |
||
| 208 | */ |
||
| 209 | protected function getTimeAttributes(NodeInterface $node, array $attributes): array |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get Attributes. |
||
| 237 | */ |
||
| 238 | 6 | protected function getTypeAttributes(NodeInterface $node, array $attributes): array |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Execute closures. |
||
| 291 | */ |
||
| 292 | 6 | View Code Duplication | protected function translateAttributes(NodeInterface $node, array $attributes): array |
| 310 | } |
||
| 311 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.