Passed
Push — master ( e83d3e...f8b104 )
by alpha
02:24
created

AliyunOssReadTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 10.4 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 13
loc 125
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 6 1
A read() 0 7 1
A readStream() 0 11 1
A listContents() 0 12 1
A getMetadata() 13 13 2
A getSize() 0 6 1
A getMimetype() 0 7 2
A getTimestamp() 0 7 2
A getVisibility() 0 18 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
namespace AlphaSnow\AliyunOss;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\Util;
7
use OSS\Core\OssException;
8
use OSS\OssClient;
9
10
trait AliyunOssReadTrait
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function has($path)
16
    {
17
        $object = $this->applyPathPrefix($path);
0 ignored issues
show
Bug introduced by
It seems like applyPathPrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
18
19
        return $this->client->doesObjectExist($this->bucket, $object);
0 ignored issues
show
Bug introduced by
The property bucket does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function read($path)
26
    {
27
        $result = $this->readObject($path);
0 ignored issues
show
Bug introduced by
It seems like readObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
        $result['contents'] = (string)$result['raw_contents'];
29
        unset($result['raw_contents']);
30
        return $result;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function readStream($path)
37
    {
38
        $result = $this->readObject($path);
0 ignored issues
show
Bug introduced by
It seems like readObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
        $result['stream'] = $result['raw_contents'];
40
        rewind($result['stream']);
41
        // Ensure the EntityBody object destruction doesn't close the stream
42
        // $result['raw_contents']->detachStream();
43
        unset($result['raw_contents']);
44
45
        return $result;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function listContents($directory = '', $recursive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $recursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        $dirObjects = $this->listDirObjects($directory, true);
0 ignored issues
show
Bug introduced by
It seems like listDirObjects() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
        $contents = $dirObjects["objects"];
55
56
        $result = array_map([$this, 'normalizeResponse'], $contents);
57
        $result = array_filter($result, function ($value) {
58
            return $value['path'] !== false;
59
        });
60
61
        return Util::emulateDirectories($result);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 View Code Duplication
    public function getMetadata($path)
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...
68
    {
69
        $object = $this->applyPathPrefix($path);
0 ignored issues
show
Bug introduced by
It seems like applyPathPrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
71
        try {
72
            $objectMeta = $this->client->getObjectMeta($this->bucket, $object);
73
        } catch (OssException $e) {
74
            $this->logErr(__FUNCTION__, $e);
0 ignored issues
show
Bug introduced by
It seems like logErr() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
            return false;
76
        }
77
78
        return $objectMeta;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getSize($path)
85
    {
86
        $object = $this->getMetadata($path);
87
        $object['size'] = $object['content-length'];
88
        return $object;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getMimetype($path)
95
    {
96
        if ($object = $this->getMetadata($path)) {
97
            $object['mimetype'] = $object['content-type'];
98
        }
99
        return $object;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getTimestamp($path)
106
    {
107
        if ($object = $this->getMetadata($path)) {
108
            $object['timestamp'] = strtotime($object['last-modified']);
109
        }
110
        return $object;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getVisibility($path)
117
    {
118
        $object = $this->applyPathPrefix($path);
0 ignored issues
show
Bug introduced by
It seems like applyPathPrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
        try {
120
            $acl = $this->client->getObjectAcl($this->bucket, $object);
121
        } catch (OssException $e) {
122
            $this->logErr(__FUNCTION__, $e);
0 ignored issues
show
Bug introduced by
It seems like logErr() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
123
            return false;
124
        }
125
126
        if ($acl == OssClient::OSS_ACL_TYPE_PUBLIC_READ) {
127
            $res['visibility'] = AdapterInterface::VISIBILITY_PUBLIC;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = 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...
128
        } else {
129
            $res['visibility'] = AdapterInterface::VISIBILITY_PRIVATE;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = 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...
130
        }
131
132
        return $res;
133
    }
134
}
135