FilteredHttpResponseParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 106
ccs 15
cts 19
cp 0.7895
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addMessage() 0 3 1
A getMessages() 0 3 1
A getFilteredRecord() 0 3 1
A usesCache() 0 3 1
A isFromCache() 0 3 2
A doFilterResponseFor() 0 3 1
A getRawResponse() 0 3 1
doFilterResponseById() 0 1 ?
getRawResponseById() 0 1 ?
1
<?php
2
3
namespace Onoi\Remi;
4
5
use Onoi\HttpRequest\HttpRequest;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 0.1
10
 *
11
 * @author mwjames
12
 */
13
abstract class FilteredHttpResponseParser implements ResponseParser {
14
15
	/**
16
	 * @var HttpRequest
17
	 */
18
	protected $httpRequest;
19
20
	/**
21
	 * @var FilteredRecord
22
	 */
23
	protected $filteredRecord;
24
25
	/**
26
	 * @var array
27
	 */
28
	private $messages = array();
29
30
	/**
31
	 * @since 0.1
32
	 *
33
	 * @param HttpRequest $httpRequest
34
	 * @param FilteredRecord $filteredRecord
35
	 */
36 23
	public function __construct( HttpRequest $httpRequest, FilteredRecord $filteredRecord ) {
37 23
		$this->httpRequest = $httpRequest;
38 23
		$this->filteredRecord = $filteredRecord;
39 23
	}
40
41
	/**
42
	 * @since 0.1
43
	 *
44
	 * @param string[] $message
45
	 */
46 1
	public function addMessage( $message ) {
47 1
		$this->messages[] = $message;
48 1
	}
49
50
	/**
51
	 * @since 0.1
52
	 *
53
	 * {@inheritDoc}
54
	 */
55 6
	public function getMessages() {
56 6
		return $this->messages;
57
	}
58
59
	/**
60
	 * @since 0.1
61
	 *
62
	 * {@inheritDoc}
63
	 */
64 21
	public function getFilteredRecord() {
65 21
		return $this->filteredRecord;
66
	}
67
68
	/**
69
	 * @since 0.1
70
	 *
71
	 * {@inheritDoc}
72
	 */
73 1
	public function usesCache() {
74 1
		return $this->isFromCache();
75
	}
76
77
	/**
78
	 * @since 0.1
79
	 *
80
	 * {@inheritDoc}
81
	 */
82 1
	public function isFromCache() {
83 1
		return method_exists( $this->httpRequest, 'isCached' ) ? $this->httpRequest->isCached() : false;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\HttpRequest\HttpRequest as the method isCached() does only exist in the following implementations of said interface: Onoi\HttpRequest\CachedCurlRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
84
	}
85
86
	/**
87
	 * @since 0.1
88
	 *
89
	 * {@inheritDoc}
90
	 */
91
	public function doFilterResponseFor( $id ) {
92
		return $this->doFilterResponseById( $id );
93
	}
94
95
	/**
96
	 * @since 0.1
97
	 *
98
	 * {@inheritDoc}
99
	 */
100
	public function getRawResponse( $id ) {
101
		return $this->getRawResponseById( $id );
102
	}
103
104
	/**
105
	 * @since 0.3
106
	 *
107
	 * {@inheritDoc}
108
	 */
109
	abstract public function doFilterResponseById( $id );
110
111
	/**
112
	 * @since 0.3
113
	 *
114
	 * {@inheritDoc}
115
	 */
116
	abstract public function getRawResponseById( $id );
117
118
}
119