Completed
Push — master ( abe227...316baf )
by Raffael
13:55 queued 08:01
created

Validator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 60
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 17
loc 60
ccs 10
cts 24
cp 0.4167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 3 36 10
A validateSecrets() 14 14 4

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
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Resource;
13
14
use InvalidArgumentException;
15
16
class Validator
17
{
18
    /**
19
     * Validate resource.
20
     */
21 16
    public static function validate(array $resource): array
22
    {
23
        $defaults = [
24 16
            'data' => [],
25
        ];
26
27 16
        $resource = array_replace_recursive($defaults, $resource);
28
29 16
        if (!isset($resource['name']) || !is_string($resource['name'])) {
30
            throw new InvalidArgumentException('name as string must be provided');
31
        }
32
33 16
        if (preg_match('/[^a-z\.\-\_0-9]/', $resource['name'])) {
34
            throw new InvalidArgumentException('resoure name can only consists from lower case alphanumeric characters and . or _ or -');
35
        }
36
37 16
        if (isset($resource['description']) && !is_string($resource['description'])) {
38
            throw new InvalidArgumentException('description must be a string');
39
        }
40
41 16 View Code Duplication
        if (isset($resource['data']) && !is_array($resource['data'])) {
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...
42
            throw new InvalidArgumentException('data must be an array');
43
        }
44
45 16
        if (isset($resource['secrets'])) {
46
            if (!is_array($resource['secrets'])) {
47
                throw new InvalidArgumentException('secrets must be an array');
48
            }
49
50
            self::validateSecrets($resource['secrets']);
51
        }
52
53 16
        $resource = array_intersect_key($resource, array_flip(['name', 'data', 'description', 'secrets', 'kind']));
54
55 16
        return $resource;
56
    }
57
58
    /**
59
     * Validate secrets.
60
     */
61 View Code Duplication
    protected static function validateSecrets(array $secrets): array
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...
62
    {
63
        foreach ($secrets as $key => $value) {
64
            if (['secret', 'key', 'to'] != array_keys($value)) {
65
                throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings');
66
            }
67
68
            if (array_filter($value, 'is_string') != $value) {
69
                throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings');
70
            }
71
        }
72
73
        return $secrets;
74
    }
75
}
76