DirectoryTreeObject   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 182
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.89%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 182
loc 182
ccs 86
cts 105
cp 0.8189
rs 9
wmc 35
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
B c() 20 20 5
A hasChildren() 4 4 1
C ls() 41 41 13
C lsInfo() 50 50 12
B lsTree() 26 26 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/*******************************************************************
3
 * Created by:  Marko Kungla @ OkramLabs on Aug 6, 2012 - 8:59:28
4
 * Contact:     [email protected] - https://okramlabs.com
5
 * @copyright   2015 OkramLabs - https://okramlabs.com
6
 * @license     MIT
7
 *
8
 * Package name:libhowi-filesystem
9
 * @category	HOWI3
10
 * @package		libhowi
11
 * @subpackage	filesystem
12
 * 
13
 * Lang:      PHP
14
 * Encoding:  UTF-8
15
 * File:      DirectoryTreeObject.inc
16
 * @link      https://
17
 ********************************************************************
18
 * Contributors:
19
 * @author Marko Kungla <[email protected]>
20
 *           Github: https://github.com/mkungla
21
 ********************************************************************
22
 * Comments:
23
 */
24
namespace HOWI3\libhowi\Filesystem\php7\Objects;
25
26
use \RecursiveDirectoryIterator;
27
use \RecursiveIteratorIterator;
28
use \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\DirectoryTreeInterface;
29
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
30
use \HOWI3\libhowi\Filesystem\php7\TraitForSharedMethods;
31
32 View Code Duplication
class DirectoryTreeObject extends RecursiveDirectoryIterator implements DirectoryTreeInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
{
34
    use TraitForResponse;
35
    use TraitForSharedMethods;
36
37
    /**
38
     *
39
     * @var array $dirkeys Reqistered basenames
40
     */
41
    private $dirkeys = [];
42
43
    /**
44
     *
45
     * {@inheritDoc}
46
     *
47
     */
48 21
    public function c($basename = false)
49
    {
50 21
        if (! empty($basename) && array_key_exists($basename, $this->dirkeys))
51 7
            $c = $this->dirkeys[$basename];
52 20
        elseif (! empty($basename) && is_dir($this->getPath() . DIRECTORY_SEPARATOR . $basename)) {
53 20
            $c = new DirectoryTreeObject($this->getPath() . DIRECTORY_SEPARATOR . $basename, 
54 20
                DirectoryTreeObject::SKIP_DOTS);
55 20
            $c->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
56 20
            $c->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
57 20
            $c->setLogFile($this->getLogFile());
58 20
            $c->setLogLevel($this->getLogLevel());
59 20
            $c->setUID($this->getUID());
60 20
            $c->setUsername($this->getUsername());
61
        } 
62
63
        else
64 1
            $c = false;
65
        
66 21
        return $this->dirkeys[$basename] = $c;
67
    }
68
69
    /**
70
     *
71
     * {@inheritDoc}
72
     *
73
     */
74 1
    public function hasChildren($allow_links = false)
75
    {
76 1
        return $this->valid();
77
    }
78
79
    /**
80
     *
81
     * {@inheritDoc}
82
     *
83
     */
84 1
    public function ls($sort = false)
85
    {
86 1
        $this->rewind();
87
        
88
        $ls = [
89 1
            'dir' => [],
90
            'link' => [],
91
            'file' => []
92
        ];
93 1
        $display = [];
94
        
95 1
        while ($this->valid()) {
96 1
            if (! $this->isDot()) {
97 1
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
98 1
                    $ls['dir'][$this->getFilename()] = $this->getType();
99
                
100 1
                elseif ($this->isLink() && ! empty($sort))
101 1
                    $ls['link'][$this->getFilename()] = $this->getType();
102
                
103 1
                elseif ($this->isFile() && ! empty($sort))
104 1
                    $ls['file'][$this->getFilename()] = $this->getType();
105
                else
106 1
                    $display[$this->getFilename()] = $this->getType();
107
                
108 1
                if ($this->isDir() || $this->isLink()) {
109 1
                    $this->dirkeys[$this->getFilename()] = $this->getChildren();
110
                }
111
            }
112
            
113 1
            $this->next();
114
        }
115 1
        if (! empty($sort)) {
116
            
117 1
            ksort($ls['dir']);
118 1
            ksort($ls['link']);
119 1
            ksort($ls['file']);
120
            
121 1
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
122
        }
123 1
        return $display;
124
    }
125
126
    /**
127
     *
128
     * {@inheritDoc}
129
     *
130
     */
131 1
    public function lsInfo($sort = false, $convert = false, $timeformat = false)
132
    {
133 1
        $this->rewind();
134
        
135
        $ls = [
136 1
            'dir' => [],
137
            'link' => [],
138
            'file' => []
139
        ];
140 1
        $display = [];
141 1
        $lsinfo = [];
142
        
143 1
        while ($this->valid()) {
144
            
145 1
            if (! $this->isDot()) {
146 1
                $cname = $this->getFilename();
147 1
                $lsinfo[$cname] = new \stdClass();
148 1
                $lsinfo[$cname]->name = $this->getFilename();
149 1
                $lsinfo[$cname]->type = $this->getType();
150 1
                $lsinfo[$cname]->size = $this->getSize($convert);
151
                
152 1
                $lsinfo[$cname]->taccess = $this->getATime($timeformat);
153 1
                $lsinfo[$cname]->tchange = $this->getCTime($timeformat);
154 1
                $lsinfo[$cname]->tmodify = $this->getMTime($timeformat);
155
                
156 1
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
157 1
                    $ls['dir'][$cname] = $lsinfo[$cname]->type;
158
                
159 1
                elseif ($this->isLink() && ! empty($sort))
160 1
                    $ls['link'][$cname] = $lsinfo[$cname]->type;
161
                
162 1
                elseif ($this->isFile() && ! empty($sort))
163 1
                    $ls['file'][$cname] = $lsinfo[$cname]->type;
164
                else
165 1
                    $display[$cname] = $lsinfo[$cname]->type;
166
            }
167 1
            $this->next();
168
        }
169 1
        if (! empty($sort)) {
170 1
            ksort($ls['dir']);
171 1
            ksort($ls['link']);
172 1
            ksort($ls['file']);
173
            
174 1
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
175
        }
176 1
        foreach ($display as $row => $type) {
177 1
            $display[$row] = $lsinfo[$row];
178
        }
179 1
        return $display;
180
    }
181
182
    /**
183
     *
184
     * {@inheritDoc}
185
     *
186
     */
187 2
    public function lsTree()
188
    {
189 2
        $DirectoryTreeObject = new RecursiveDirectoryIterator($this->getPath(), 
190 2
            RecursiveDirectoryIterator::SKIP_DOTS);
191 2
        $DirectoryTreeObject->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
192
        
193 2
        $ritit = new RecursiveIteratorIterator($DirectoryTreeObject, RecursiveIteratorIterator::CHILD_FIRST);
194 2
        $tree = array();
195 2
        foreach ($ritit as $splFileInfo) {
196 2
            $path = $splFileInfo->isDir() ? array(
197 2
                $splFileInfo->getFilename() => array()
198 2
            ) : array(
199 2
                $splFileInfo->getFilename()
200
            );
201
            
202 2
            for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth --) {
203
                $path = array(
204 2
                    $ritit->getSubIterator($depth)
205 2
                        ->current()
206 2
                        ->getFilename() => $path
207
                );
208
            }
209 2
            $tree = array_merge_recursive($tree, $path);
210
        }
211 2
        return $tree;
212
    }
213
}
214