Passed
Push — develop ( d1d076...95966b )
by Jens
03:19
created

AbstractStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 35.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 62
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validateFilename() 22 30 4
A save() 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
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace library\storage\storage;
7
8
9
use library\storage\Repository;
10
11
abstract class AbstractStorage
12
{
13
	/**
14
	 * @var Repository
15
	 */
16
	protected $repository;
17
18
	public function __construct($repository)
19
	{
20
		$this->repository = $repository;
21
	}
22
23
	/**
24
	 * Converts filename to lowercase, remove non-ascii chars
25
	 * And adds "-copy" if the file already exists
26
	 *
27
	 * @param $filename
28
	 * @param $path
29
	 *
30
	 * @return string
31
	 */
32
	protected function validateFilename($filename, $path)
33
	{
34
		$fileParts = explode('.', $filename);
35 View Code Duplication
		if (count($fileParts) > 1) {
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...
36
			$extension = end($fileParts);
37
			array_pop($fileParts);
38
			$fileNameWithoutExtension = implode('-', $fileParts);
39
			$fileNameWithoutExtension = slugify($fileNameWithoutExtension);
40
			$filename = $fileNameWithoutExtension . '.' . $extension;
41
		} else {
42
			$filename = slugify($filename);
43
		}
44
45 View Code Duplication
		if (file_exists($path . '/' . $filename)) {
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...
46
			$fileParts = explode('.', $filename);
47
			if (count($fileParts) > 1) {
48
				$extension = end($fileParts);
49
				array_pop($fileParts);
50
				$fileNameWithoutExtension = implode('-', $fileParts);
51
				$fileNameWithoutExtension .= '-copy';
52
				$filename = $fileNameWithoutExtension . '.' . $extension;
53
			} else {
54
				$filename .= '-copy';
55
			}
56
57
			return $this->validateFilename($filename, $path);
58
		}
59
60
		return $filename;
61
	}
62
63
	/**
64
	 * Save changes made to the repository
65
	 *
66
	 * @throws \Exception
67
	 */
68
	protected function save()
69
	{
70
		$this->repository->save();
71
	}
72
}