Completed
Push — fix-clean-code ( fbbd48...6c73f0 )
by Arnaud
02:50 queued 01:07
created

Util   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 9.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 14
loc 152
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFS() 0 8 2
A runGitCommand() 0 19 3
A sortByDate() 14 14 5
A dateIsValid() 0 6 2
A dateToDatetime() 0 15 4
A formatClassName() 0 12 2
A arraySearchByKey() 0 14 3

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
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Process\Process;
13
14
class Util
15
{
16
    /**
17
     * Symfony\Component\Filesystem.
18
     *
19
     * @var Filesystem
20
     */
21
    protected static $fs;
22
23
    /**
24
     * Return Symfony\Component\Filesystem instance.
25
     *
26
     * @return Filesystem
27
     */
28
    public static function getFS()
29
    {
30
        if (!self::$fs instanceof Filesystem) {
31
            self::$fs = new Filesystem();
32
        }
33
34
        return self::$fs;
35
    }
36
37
    /**
38
     * Runs a Git command on the repository.
39
     *
40
     * @param string $command The command.
41
     *
42
     * @throws \RuntimeException If the command failed.
43
     *
44
     * @return string The trimmed output from the command.
45
     */
46
    public static function runGitCommand($command)
47
    {
48
        try {
49
            $process = new Process($command, __DIR__);
0 ignored issues
show
Documentation introduced by
$command is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
            if (0 === $process->run()) {
51
                return trim($process->getOutput());
52
            }
53
54
            throw new \RuntimeException(
55
                sprintf(
56
                    'The tag or commit hash could not be retrieved from "%s": %s',
57
                    __DIR__,
58
                    $process->getErrorOutput()
59
                )
60
            );
61
        } catch (\RuntimeException $exception) {
62
            throw new \RuntimeException('Process error');
63
        }
64
    }
65
66
    /**
67
     * Sort array items by date.
68
     *
69
     * @param $a
70
     * @param $b
71
     *
72
     * @return int
73
     */
74 View Code Duplication
    public static function sortByDate($a, $b)
75
    {
76
        if (!isset($a['date'])) {
77
            return -1;
78
        }
79
        if (!isset($b['date'])) {
80
            return 1;
81
        }
82
        if ($a['date'] == $b['date']) {
83
            return 0;
84
        }
85
86
        return ($a['date'] > $b['date']) ? -1 : 1;
87
    }
88
89
    /**
90
     * Checks if a date is valid.
91
     *
92
     * @param string $date
93
     * @param string $format
94
     *
95
     * @return bool
96
     */
97
    public static function dateIsValid(string $date, string $format = 'Y-m-d'): bool
0 ignored issues
show
Coding Style introduced by
function dateIsValid() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
98
    {
99
        $d = \DateTime::createFromFormat($format, $date);
100
101
        return $d && $d->format($format) === $date;
102
    }
103
104
    /**
105
     * Date to DateTime.
106
     *
107
     * @param mixed $date
108
     *
109
     * @return \DateTime
110
     */
111
    public static function dateToDatetime($date): \DateTime
112
    {
113
        // DateTime
114
        if ($date instanceof \DateTime) {
115
            return $date;
116
        }
117
        // timestamp or AAAA-MM-DD
118
        if (is_numeric($date)) {
119
            return (new \DateTime())->setTimestamp($date);
120
        }
121
        // string (ie: '01/01/2019', 'today')
122
        if (is_string($date)) {
123
            return new \DateTime($date);
124
        }
125
    }
126
127
    /**
128
     * Format class name.
129
     *
130
     * @param \object $class
131
     * @param array   $options
132
     *
133
     * @return string
134
     */
135
    public static function formatClassName($class, array $options = []): string
136
    {
137
        $lowercase = false;
138
        extract($options);
139
140
        $className = substr(strrchr(get_class($class), '\\'), 1);
141
        if ($lowercase) {
142
            $className = strtolower($className);
143
        }
144
145
        return $className;
146
    }
147
148
    /**
149
     * search multidimensional array for key and return value.
150
     */
151
    public function arraySearchByKey(string $keyVal, array $array)
152
    {
153
        foreach ($array as $key => $val) {
154
            if ($keyVal == $key) {
155
                $resultSet['name'] = $val['name'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$resultSet was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resultSet = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
156
                $resultSet['key'] = $key;
157
                $resultSet['id'] = $val['id'];
158
159
                return $resultSet;
160
            }
161
        }
162
163
        return null;
164
    }
165
}
166