Failed Conditions
Push — future/ConfuenceWrapper ( 63d44f...28d061 )
by
unknown
03:28
created

Confluence   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 1
cbo 6
dl 0
loc 196
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B createNewPage() 0 48 5
B getPageId() 0 46 4
A getBase64Credentials() 0 10 1
A getHeaders() 0 9 1
B getBody() 0 28 2
A getRequest() 0 5 1
1
<?php
2
3
namespace CodeMine\ConfluenceImporter\Service;
4
5
use CodeMine\ConfluenceImporter\Documentation\PageInterface;
6
use CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface;
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\Exception\ClientException;
10
use GuzzleHttp\Psr7\Request;
11
use GuzzleHttp\Psr7\Stream;
12
13
/**
14
 * Class Confluence
15
 *
16
 * @package CodeMine\ConfluenceImporter\Service
17
 */
18
class Confluence
19
{
20
    const ADD_PAGE = 'rest/api/content';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21
    const SEARCH_PAGE = 'rest/api/content/search?cql=title="%s" and space="%s"';
22
23
    /**
24
     * @var \GuzzleHttp\ClientInterface
25
     */
26
    private $client;
27
    /**
28
     * @var \CodeMine\ConfluenceImporter\Service\InstanceInterface
29
     */
30
    private $confluenceInstance;
31
32
    /**
33
     * Confluence constructor.
34
     *
35
     * @param \GuzzleHttp\ClientInterface $client
36
     * @param \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface $confluenceInstance
37
     */
38
    public function __construct(ClientInterface $client, InstanceInterface $confluenceInstance)
39
    {
40
        $this->client = $client;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
        $this->confluenceInstance = $confluenceInstance;
0 ignored issues
show
Documentation Bug introduced by
It seems like $confluenceInstance of type object<CodeMine\Confluen...ence\InstanceInterface> is incompatible with the declared type object<CodeMine\Confluen...vice\InstanceInterface> of property $confluenceInstance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
    }
43
44
    public function createNewPage(string $key, PageInterface $page, PageInterface $parentPage = NULL)
45
    {
46
47
        $body = $this->getBody($key, $page, $parentPage);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
        $headers = $this->getHeaders();
49
50
51
52
        try {
53
            $request = $this->getRequest($headers, $body);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
            $response = $this->client->send($request); //TODO: find solution for exception on same page name
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
            $pageChildren = $page->children();
56
            if (null !== $pageChildren){
57
                var_dump('1');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('1'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
58
                var_dump(urlencode('"sda sd"'));die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method createNewPage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
59
                foreach($pageChildren as $pageChild){
0 ignored issues
show
Unused Code introduced by
foreach ($pageChildren a...>getMessage()); } } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
60
                    var_dump('2');
61
62
                    $childBody = $this->getBody($key, $pageChild, $page);
0 ignored issues
show
Unused Code introduced by
$childBody 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...
63
                    var_dump('child body');
64
                    $childRequest = $this->getRequest($headers, $pageChild);
0 ignored issues
show
Unused Code introduced by
$childRequest 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...
65
                    try {
66
                        var_dump('3');
67
68
                        $this->client->send($request);
69
                    }catch(\Exception $e){
70
                        var_dump($e->getMessage());
71
                    }
72
                }
73
74
                var_dump(urlencode('"sda sd"'));die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method createNewPage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
75
76
            }
77
//            var_dump($response->);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
        }catch(ClientException $e){
79
80
            echo 'ERROR!';
81
            var_dump($e->getMessage());
82
            echo 'ERROR!!!';
83
84
            return FALSE;
85
        }
86
        $returnBody = $response->getBody();
87
        $stdBody = json_decode($returnBody->getContents());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
88
        $pageId = (string)$stdBody->id;
0 ignored issues
show
Unused Code introduced by
$pageId 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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
90
        return TRUE;
91
    }
92
93
    public function getPageId(string $key, PageInterface $page) //TODO::Change for private method
94
    {
95
        $headers = [
96
            'Authorization' => 'Basic ' . $this->getBase64Credentials(),
97
        ];
98
99
        $url = sprintf(Confluence::SEARCH_PAGE, $page->title(), $key);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
100
        var_dump($url);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($url); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
101
102
103
        var_dump('ID');
104
        $request = new Request('GET', $url, $headers);
105
106
        try {
107
            var_dump('try');
108
//var_dump($request);die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
            $response = $this->client->send($request);
110
        }catch(\Exception $e){
111
            var_dump('EEEEEEEEEEEEEEEERRRROOOOOOOOOOOOR');
112
            var_dump($e->getMessage());
113
            die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getPageId() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
114
        }
115
var_dump($response->getBody());die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getPageId() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
116
        /** @var Stream $returnBody */
117
        $returnBody = $response->getBody();
0 ignored issues
show
Unused Code introduced by
/** @var Stream $returnB...= $response->getBody(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
118
        $returnBody->
119
        $e = $response->getHeaders();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
120
121
        $stdBody = json_decode($returnBody->getContents());
122
123
//        var_dump($stdBody);
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
//        var_dump($response->getStatusCode());
125
//        var_dump(count($stdBody->results));
126
//        var_dump($stdBody->results);
127
128
        if ($response->getStatusCode() == 200 && count($stdBody->results) == 1){
0 ignored issues
show
Bug introduced by
The variable $response seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $stdBody seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
129
130
            $id = (string)$stdBody->results[0]->id;
131
132
            return $id;
133
134
        }
135
136
        throw new \Exception('ERROR!!!!');
137
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    private function getBase64Credentials()
144
    {
145
        $base64Credentials = base64_encode(
146
            $this->confluenceInstance->username() .
147
            ':' .
148
            $this->confluenceInstance->password()
149
        );
150
151
        return $base64Credentials;
152
    }
153
154
    /**
155
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
156
     */
157
    private function getHeaders()
158
    {
159
        $headers = [
160
            'Authorization' => 'Basic ' . $this->getBase64Credentials(),
161
            'Content-Type' => 'application/json'
162
        ];
163
164
        return $headers;
165
    }
166
167
    /**
168
     * @param string $key
169
     * @param PageInterface $page
170
     * @param PageInterface|NULL $parentPage
171
     * @return string<json>
0 ignored issues
show
Documentation introduced by
The doc-type string<json> could not be parsed: Expected "|" or "end of type", but got "<" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
172
     * @throws \Exception
173
     */
174
    private function getBody(string $key, PageInterface $page, PageInterface $parentPage = NULL)
175
    {
176
        $bodyArray = [
177
            'type' => 'page',
178
            'title' => $page->title(),
179
            'space' => [
180
                'key' => $key
181
            ],
182
            'body' => [
183
                'storage' => [
184
                    'value' => $page->content(),
185
                    'representation' => 'storage'
186
                ]
187
            ]
188
        ];
189
190
        if (isset($parentPage)) {
191
            $parentPageId = $this->getPageId($key, $parentPage);
192
193
            $bodyArray['ancestors'] = [
194
                ['id' => $parentPageId]
195
            ];
196
        }
197
198
        $body = json_encode($bodyArray);
199
200
        return $body;
201
    }
202
203
    /**
204
     * @param $headers
205
     * @param $body
206
     * @return Request
207
     */
208
    private function getRequest($headers, $body)
209
    {
210
        $request = new Request('POST', Confluence::ADD_PAGE, $headers, $body);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
211
        return $request;
212
    }
213
}
214
/*
215
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'
216
{"type":"page",
217
"title":"new page",
218
 ancestors:[{"id":456}], "space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>",
219
"representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool
220
*/