DirectoryTreeObject::ls()   C
last analyzed

Complexity

Conditions 13
Paths 20

Size

Total Lines 41
Code Lines 26

Duplication

Lines 41
Ratio 100 %

Code Coverage

Tests 29
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 26
c 1
b 0
f 0
nc 20
nop 1
dl 41
loc 41
ccs 29
cts 29
cp 1
crap 13
rs 5.1234

How to fix   Complexity   

Long Method

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:

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