NormalContentsFileControllerTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 12 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 1
dl 12
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalLoader() 0 22 4
A normalTmpFilePath() 0 4 1
A normalFilePath() 12 12 4
B normalResizeSet() 0 26 7

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 ContentsFile\Controller\Traits;
4
5
use Cake\Core\Configure;
6
use Cake\ORM\Entity;
7
8
/**
9
 * 通常のファイルローダー周り
10
 * NormalContentsFileControllerTrait
11
 */
12
trait NormalContentsFileControllerTrait
13
{
14
    /**
15
     * normalLoader
16
     * 通常のローダー
17
     * @param string $filename
18
     * @param string $filepath
19
     * @return void
20
     * @author hagiwara
21
     */
22
    private function normalLoader(string $filename, string $filepath): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
    {
24
        $fileExt = null;
25
        if (preg_match('/\.([^\.]*)$/', $filename, $ext)) {
26
            if ($ext[1]) {
27
                $fileExt = strtolower($ext[1]);
28
            }
29
        }
30
31
        $this->response = $this->response->withHeader('Content-Length', filesize($filepath));
0 ignored issues
show
Bug introduced by
The property response 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...
32
        if (!empty($fileExt)) {
33
            $fileContentType = $this->getFileType($fileExt);
0 ignored issues
show
Bug introduced by
It seems like getFileType() 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...
34
        } else {
35
            $fileContentType = $this->getMimeType($filepath);
0 ignored issues
show
Bug introduced by
It seems like getMimeType() 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...
36
        }
37
        $this->response = $this->response->withType($fileContentType);
38
        @ob_end_clean(); // clean
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
        $fp = fopen($filepath, 'r');
40
        $body = fread($fp, filesize($filepath));
41
        fclose($fp);
42
        $this->response->getBody()->write($body);
43
    }
44
45
    /**
46
     * normalTmpFilePath
47
     * 通常用のtmpのパス作成
48
     * @author hagiwara
49
     * @param string $filename
50
     * @return string
51
     */
52
    private function normalTmpFilePath(string $filename): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
53
    {
54
        return Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
55
    }
56
57
    /**
58
     * normalFilePath
59
     * 通常用のファイルのパス作成
60
     * @author hagiwara
61
     * @param Entity $attachmentData
62
     * @return string
63
     */
64 View Code Duplication
    private function normalFilePath(Entity $attachmentData): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
65
    {
66
        $ext = '';
67
        if (Configure::read('ContentsFile.Setting.ext') === true) {
68
            $ext = '.' . (new \SplFileInfo($attachmentData->file_name))->getExtension();
69
        }
70
        if (Configure::read('ContentsFile.Setting.randomFile') === true && $attachmentData->file_random_path != '') {
71
            return Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->file_random_path . $ext;
72
        } else {
73
            return Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->field_name . $ext;
74
        }
75
    }
76
77
    /**
78
     * normalResizeSet
79
     * 通常のリサイズ処理
80
     * @author hagiwara
81
     * @param string $filepath
82
     * @param array $resize
83
     * @return string
84
     */
85
    private function normalResizeSet(string $filepath, array $resize): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
86
    {
87
        if (empty($resize['width'])) {
88
            $resize['width'] = 0;
89
        }
90
        if (empty($resize['height'])) {
91
            $resize['height'] = 0;
92
        }
93
        //両方ゼロの場合はそのまま返す
94
        if ($resize['width'] == 0 && $resize['height'] == 0) {
95
            return $filepath;
96
        }
97
        $imagepathinfo = $this->baseModel->getPathinfo($filepath, $resize);
0 ignored issues
show
Bug introduced by
The property baseModel 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...
98
99
        //ファイルの存在チェック
100
        if (file_exists($imagepathinfo['resize_filepath'])) {
101
            return $imagepathinfo['resize_filepath'];
102
        }
103
104
        //ない場合はリサイズを実行
105
        if (!$this->baseModel->imageResize($filepath, $resize)) {
106
            //失敗時はそのままのパスを返す(画像以外の可能性あり)
107
            return $filepath;
108
        }
109
        return $imagepathinfo['resize_filepath'];
110
    }
111
}
112