Failed Conditions
Pull Request — master (#6)
by Sander
01:54
created

Evernote   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 54
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 19
loc 54
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
D checkEvernote() 19 47 17

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
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\OwnNote\Utility;
25
26
27
use DOMDocument;
28
use OC\Files\Filesystem;
29
30
class Evernote {
31
32
	/**
33
	 * @param $folder
34
	 * @param string $file
35
	 */
36
	public static function checkEvernote($folder, $file) {
37
		$utils = new Utils();
38
		$html = "";
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
		if ($html = Filesystem::file_get_contents($folder . "/" . $file)) {
40
			$DOM = new DOMDocument;
41
			$DOM->loadHTML($html);
42
			$items = $DOM->getElementsByTagName('meta');
43
			$isEvernote = false;
44 View Code Duplication
			for ($i = 0; $i < $items->length; $i++) {
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...
45
				$item = $items->item($i);
46
				if ($item->hasAttributes()) {
47
					$attrs = $item->attributes;
48
					foreach ($attrs as $a => $attr) {
49
						if ($attr->name == "name") {
50
							if ($attr->value == "exporter-version" || $attr->value == "Generator") {
51
								$isEvernote = true;
52
								continue;
53
							}
54
						}
55
					}
56
				}
57
			}
58
			if ($isEvernote) {
59
				$items = $DOM->getElementsByTagName('img');
60
				$isEvernote = false;
0 ignored issues
show
Unused Code introduced by
$isEvernote is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
				for ($i = 0; $i < $items->length; $i++) {
62
					$item = $items->item($i);
63
					if ($item->hasAttributes()) {
64
						$attrs = $item->attributes;
65
						foreach ($attrs as $a => $attr) {
66
							if ($attr->name == "src") {
67
								$url = $attr->value;
68
								if (!$utils->startsWith($url, "http") && !$utils->startsWith($url, "/") && !$utils->startsWith($url, "data")) {
69 View Code Duplication
									if ($data = Filesystem::file_get_contents($folder . "/" . $url)) {
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...
70
										$type = pathinfo($url, PATHINFO_EXTENSION);
71
										$base64 = "data:image/" . $type . ";base64," . base64_encode($data);
72
										$html = str_replace($url, $base64, $html);
73
									}
74
								}
75
							}
76
						}
77
					}
78
				}
79
				Filesystem::file_put_contents($folder . "/" . $file, $html);
80
			}
81
		}
82
	}
83
}