Completed
Push — master ( 8eade9...5ff424 )
by Matt
04:58
created

ClientResult::hasDebugHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
namespace Gothick\AkismetClient;
3
4
abstract class ClientResult
5
{
6
7
	const PRO_TIP_HEADER = 'X-akismet-pro-tip';
8
9
	/**
10
	 * @var string Raw string we got back from the Akismet API as an answer
11
	 */
12
	protected $raw_result = '';
13
14
	/**
15
	 * @var string Akismet's X-akismet-pro-tip header, which sometimes has useful extra information.
16
	 */
17
	protected $pro_tip = '';
18
19
	/**
20
	 * @var string If there was an X-akismet-debug-header, this is what it contained.
21
	 */
22
	protected $debug_help;
23
24 4
	public function __construct(\GuzzleHttp\Psr7\Response $response)
25
	{
26 4
		if ($response->hasHeader('X-akismet-debug-help'))
27
		{
28
			$this->debug_help = $response->getHeader('X-akismet-debug-help');
0 ignored issues
show
Documentation Bug introduced by
It seems like $response->getHeader('X-akismet-debug-help') can also be of type array. However, the property $debug_help is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
		}
30 4
		if ($response->hasHeader(self::PRO_TIP_HEADER))
31
		{
32
			$this->pro_tip = $response->getHeader(self::PRO_TIP_HEADER);
0 ignored issues
show
Documentation Bug introduced by
It seems like $response->getHeader(self::PRO_TIP_HEADER) can also be of type array. However, the property $pro_tip is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
33
		}
34
35 4
		if ($response->getStatusCode() != 200)
36
		{
37
			// Our clients are meant to check first
38
			$message = 'Response with invalid status code in ' . __METHOD__;
39
			if ($this->hasDebugHelp())
40
			{
41
				$message .= ' (debug help: ' . $this->getDebugHelp() . ')';
0 ignored issues
show
Bug introduced by
Are you sure $this->getDebugHelp() of type mixed|array can be used in concatenation? ( Ignorable by Annotation )

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

41
				$message .= ' (debug help: ' . /** @scrutinizer ignore-type */ $this->getDebugHelp() . ')';
Loading history...
42
			}
43
			throw new Exception($message);
44
		}
45 4
		$this->raw_result = (string) $response->getBody();
46 4
	}
47
48
	public function hasProTip()
49
	{
50
		return !empty($this->pro_tip);
51
	}
52
	public function getProTip()
53
	{
54
		return $this->pro_tip;
55
	}
56
	public function hasDebugHelp()
57
	{
58
		return !empty($this->debug_help);
59
	}
60
	public function getDebugHelp()
61
	{
62
		return $this->pro_tip;
63
	}
64
}
65