Passed
Pull Request — master (#34)
by Charis
01:35
created

createArticleFromArray()   F

Complexity

Conditions 13
Paths 4096

Size

Total Lines 27
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 4096
nop 1
dl 0
loc 27
rs 2.45
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace One;
4
5
use one\Model\Article;
6
7
/**
8
 * createUriFromString
9
 *
10
 * @param string $uri
11
 * @return \Psr\Http\Message\UriInterface
12
 */
13
function createUriFromString($uri)
14
{
15
    $parts = parse_url($uri);
16
    $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
17
    $user = isset($parts['user']) ? $parts['user'] : '';
18
    $pass = isset($parts['pass']) ? $parts['pass'] : '';
19
    $host = isset($parts['host']) ? $parts['host'] : '';
20
    $port = isset($parts['port']) ? $parts['port'] : null;
21
    $path = isset($parts['path']) ? $parts['path'] : '';
22
    $query = isset($parts['query']) ? $parts['query'] : '';
23
    $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
24
    return new Uri(
25
        $scheme,
26
        $host,
27
        $port,
28
        $path,
29
        $query,
30
        $fragment,
31
        $user,
32
        $pass
33
    );
34
}
35
36
/**
37
 * createuriFromServer
38
 *
39
 * @return \Psr\Http\Message\UriInterface
40
 */
41
42
function createuriFromServer()
43
{
44
    $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
45
    $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
46
    $port = empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
47
    $path = (string) parse_url('http://www.example.com/' . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
48
    $query = empty($_SERVER['QUERY_STRING']) ? parse_url('http://example.com' . $_SERVER['REQUEST_URI'], PHP_URL_QUERY) : $_SERVER['QUERY_STRING'];
49
    $fragment = '';
50
    $user = !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
51
    $password = !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
52
    if (empty($user) && empty($password) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
53
        list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
54
    }
55
56
    return new Uri(
57
        $scheme,
58
        $host,
59
        $port,
60
        $path,
61
        $query,
62
        $fragment,
63
        $user,
64
        $password
65
    );
66
}
67
/**
68
 * createArticleFromArray
69
 *
70
 * @param array $data
71
 */
72
function createArticleFromArray($data)
73
{
74
    $title = isset($data['title']) ? $data['title'] : '';
75
    $body = isset($data['body']) ? $data['body'] : '';
76
    $source = isset($data['source']) ? $data['source'] : '';
77
    $uniqueId = isset($data['uniqueId']) ? $data['uniqueId'] : '';
78
    $typeId = isset($data['typeId']) ? $data['typeId'] : null;
79
    $categoryId = isset($data['categoryId']) ? $data['categoryId'] : null;
80
    $reporter = isset($data['reporter']) ? $data['reporter'] : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $reporter is dead and can be removed.
Loading history...
81
    $lead = isset($data['lead']) ? $data['lead'] : '';
82
    $reporter = isset($data['reporter']) ? $data['reporter'] : '';
83
    $tags = isset($data['tags']) ? $data['tags'] : '';
84
    $publishedAt = isset($data['publishedAt']) ? $data['publishedAt'] : null;
85
    $identifier = isset($data['identifier']) ? $data['identifier'] : null;
86
    return new Article(
87
        $title,
88
        $body,
89
        $source,
90
        $uniqueId,
91
        $typeId,
92
        $categoryId,
93
        $reporter,
94
        $lead,
95
        $reporter,
96
        $tags,
97
        $publishedAt,
98
        $identifier
0 ignored issues
show
Unused Code introduced by
The call to one\Model\Article::__construct() has too many arguments starting with $identifier. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
    return /** @scrutinizer ignore-call */ new Article(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
99
    );
100
}
101