Configuration   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 130
Duplicated Lines 13.85 %

Coupling/Cohesion

Components 6
Dependencies 1

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 25
lcom 6
cbo 1
dl 18
loc 130
rs 9.0909
c 0
b 0
f 0
ccs 46
cts 52
cp 0.8846

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setMid() 0 8 2
C setRurl() 0 23 7
A setKey() 9 9 2
A setLang() 9 9 2
A setRem() 0 8 2
A setButtonTemplate() 0 8 2
A getButtonTemplate() 0 4 1
A __call() 0 4 1
B getNormalizedKey() 0 14 5

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 PaySys\TatraPay;
4
5
use Nette;
6
use Nette\Application\LinkGenerator;
7
use Nette\Utils\Strings;
8
use PaySys\PaySys\IConfiguration;
9
10
11
/**
12
 * @method string getMid()
13
 * @method string getRurl()
14
 * @method string getKey()
15
 * @method string getIpc()
16
 * @method string getLang()
17
 * @method string getMode()
18
 * @method string getRem()
19
 */
20 1
final class Configuration implements IConfiguration
21
{
22
23
	/** @var string */
24
	private $mid;
25
26
	/** @var string */
27
	private $rurl;
28
29
	/** @var string */
30
	private $lang = 'sk';
31
32
	/** @var string */
33
	private $key;
34
35
	/** @var string */
36
	private $rem = '';
37
38
	/** @var LinkGenerator */
39
	private $linkGenerator;
40
41
	/** @var string */
42
	private $buttonTemplate;
43
44
45
	public function __construct(string $mid, $rurl, string $key, LinkGenerator $linkGenerator = NULL)
46
	{
47 1
		$this->linkGenerator = $linkGenerator;
48 1
		$this->setMid($mid);
49 1
		$this->setRurl($rurl);
50 1
		$this->setKey($key);
51 1
		$this->setButtonTemplate(__DIR__ . '/template/button.latte');
52 1
	}
53
54
	public function setMid(string $mid) : Configuration
55
	{
56 1
		if (!Validator::isMid($mid))
57 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Parameter MID must have 3 or 4 characters. '%s' is invalid.", $mid));
58
59 1
		$this->mid = $mid;
60 1
		return $this;
61
	}
62
63
	public function setRurl($originalRurl) : Configuration
64
	{
65 1
		$supportedTypes = ['string', 'array'];
66 1
		if (!in_array(gettype($originalRurl), $supportedTypes))
67 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL type of '%s' is invalid. Must be %s.", gettype($originalRurl), implode(' or ', $supportedTypes)));
68
69 1
		$rurl = $originalRurl;
70 1
		if ($this->linkGenerator instanceof LinkGenerator) {
0 ignored issues
show
Bug introduced by
The class Nette\Application\LinkGenerator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
71
			try {
72 1
				if (is_string($originalRurl)) {
73 1
					$rurl = $this->linkGenerator->link($originalRurl);
74
				} elseif (is_array($originalRurl)) {
75 1
					$rurl = $this->linkGenerator->link($originalRurl['dest'], @$originalRurl['params']);
76
				}
77 1
			} catch (Nette\Application\UI\InvalidLinkException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Nette\Application\UI\InvalidLinkException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
78
		}
79
80 1
		if (!Validator::isRurl($rurl))
81 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL '%s' is invalid. Must be valid URL by RFC 1738.", $originalRurl));
82
83 1
		$this->rurl = $rurl;
84 1
		return $this;
85
	}
86
87 View Code Duplication
	public function setKey(string $originalKey) : Configuration
1 ignored issue
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...
88
	{
89 1
		$key = $this->getNormalizedKey($originalKey);
90 1
		if (!Validator::isKey($key))
91
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Key '%s' is invalid. Must have 64 byte standard string or 128 byte in hexadecimal format.", $originalKey));
92
93 1
		$this->key = $key;
94 1
		return $this;
95
	}
96
97 View Code Duplication
	public function setLang(string $originalLang) : Configuration
1 ignored issue
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...
98
	{
99 1
		$lang = strtolower($originalLang);
100 1
		if (!Validator::isLang($lang))
101 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Lang '%s' is not supported.", $originalLang));
102
103 1
		$this->lang = $lang;
104 1
		return $this;
105
	}
106
107
	public function setRem(string $rem) : Configuration
108
	{
109
		if (!Nette\Utils\Validators::isEmail($rem))
110
			throw new \PaySys\PaySys\ConfigurationException(sprintf("'%s' is not valid e-mail.", $rem));
111
112
		$this->rem = $rem;
113
		return $this;
114
	}
115
116
	public function setButtonTemplate(string $path) : Configuration
117
	{
118 1
		if (!file_exists($path))
119 1
			throw new \PaySys\PaySys\ConfigurationException(sprintf("Template file '%s' not exists.", $path));
120
121 1
		$this->buttonTemplate = $path;
122 1
		return $this;
123
	}
124
125
	public function getButtonTemplate() : string
126
	{
127 1
		return $this->buttonTemplate;
128
	}
129
130
	public function __call($method, $arguments) : string
131
	{
132 1
		return $this->{strtolower(substr($method, 3))};
133
	}
134
135
	private function getNormalizedKey(string $originalKey) : string
136
	{
137 1
		if (strlen($originalKey) === 128 OR strlen($originalKey) === 191) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
138 1
			if (strlen($originalKey) === 191) {
139 1
				$key = str_replace(':', '', $originalKey);
140
			} else {
141 1
				$key = $originalKey;
142
			}
143
144 1
			if (strlen($key) === 128)
145 1
				return pack("H*", $key);
146
		}
147 1
		return $originalKey;
148
	}
149
}
150