Completed
Pull Request — master (#132)
by
unknown
06:43 queued 04:56
created

BSDTarOutputParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 24.49 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 24
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parseFileListing() 12 60 7
A parseInflatorVersion() 12 12 2
A parseDeflatorVersion() 0 4 1

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
/*
4
 * This file is part of Zippy.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Alchemy\Zippy\Parser;
12
13
use Alchemy\Zippy\Exception\RuntimeException;
14
15
/**
16
 * This class is responsible of parsing GNUTar command line output
17
 */
18
class BSDTarOutputParser implements ParserInterface
19
{
20
    const PERMISSIONS   = '([ldrwx-]+)';
21
    const HARD_LINK     = '(\d+)';
22
    const OWNER         = '([a-z][-a-z0-9]*)';
23
    const GROUP         = '([a-z][-a-z0-9]*)';
24
    const FILESIZE      = '(\d*)';
25
    const DATE          = '([a-zA-Z0-9]+\s+[a-z0-9]+\s+[a-z0-9:]+)';
26
    const FILENAME      = '(.*)';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function parseFileListing($output)
32
    {
33
        $lines = array_values(array_filter(explode("\n", $output)));
34
        $members = array();
35
36
        // BSDTar outputs two differents format of date according to the mtime
37
        // of the member. If the member is younger than six months the year is not shown.
38
        // On 4.5+ FreeBSD system the day is displayed first
39
        $dateFormats = array('M d Y', 'M d H:i', 'd M Y', 'd M H:i');
40
41
        foreach ($lines as $line) {
42
            $matches = array();
43
44
            // drw-rw-r--  0 toto titi     0 Jan  3  1980 practice/
45
            // -rw-rw-r--  0 toto titi     10240 Jan 22 13:31 practice/records
46 View Code Duplication
            if (!preg_match_all("#" .
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
                self::PERMISSIONS . "\s+" . // match (drw-r--r--)
48
                self::HARD_LINK . "\s+" . // match (1)
49
                self::OWNER . "\s" . // match (toto)
50
                self::GROUP . "\s+" . // match (titi)
51
                self::FILESIZE . "\s+" . // match (0)
52
                self::DATE . "\s+" . // match (Jan  3  1980)
53
                self::FILENAME . // match (practice)
54
                "#", $line, $matches, PREG_SET_ORDER
55
            )) {
56
                continue;
57
            }
58
59
            $chunks = array_shift($matches);
60
61
            if (8 !== count($chunks)) {
62
                continue;
63
            }
64
65
            $date = null;
66
67
            foreach ($dateFormats as $format) {
68
                $date = \DateTime::createFromFormat($format, $chunks[6]);
69
70
                if (false === $date) {
71
                    continue;
72
                } else {
73
                    break;
74
                }
75
            }
76
77
            if (false === $date) {
78
                throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line));
79
            }
80
81
            $members[] = array(
82
                'location'  => $chunks[7],
83
                'size'      => $chunks[5],
84
                'mtime'     => $date,
85
                'is_dir'    => 'd' === $chunks[1][0]
86
            );
87
        }
88
89
        return $members;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 View Code Duplication
    public function parseInflatorVersion($output)
0 ignored issues
show
Duplication introduced by
This method 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...
96
    {
97
        $chunks = explode(' ', $output, 3);
98
99
        if (2 > count($chunks)) {
100
            return null;
101
        }
102
103
        list(, $version) = explode(' ', $output, 3);
104
105
        return $version;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function parseDeflatorVersion($output)
112
    {
113
        return $this->parseInflatorVersion($output);
114
    }
115
}
116