Output::header()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Debugger\Values;
4
5
use App\Ship\Parents\Values\Value;
6
use Illuminate\Support\Facades\Config;
7
use Jenssegers\Agent\Facades\Agent;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Class Output
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16
class Output extends Value
17
{
18
19
    /**
20
     * @var  string
21
     */
22
    public $output = '';
23
24
    /**
25
     * @var
26
     */
27
    private $request;
28
29
    /**
30
     * @var
31
     */
32
    private $response;
33
34
    /**
35
     * @var
36
     */
37
    protected $responseDataCut;
38
39
    /**
40
     * @var
41
     */
42
    protected $tokenDataCut;
43
44
    /**
45
     * Output constructor.
46
     *
47
     * @param \Symfony\Component\HttpFoundation\Request  $request
48
     * @param \Symfony\Component\HttpFoundation\Response $response
49
     */
50
    public function __construct(Request $request, Response $response)
0 ignored issues
show
Bug introduced by Mahmoud Zalt
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
51
    {
52
        $this->request = $request;
53
        $this->response = $response;
54
55
        $this->responseDataCut = Config::get("debugger.requests.response_show_first");
56
        $this->tokenDataCut = Config::get("debugger.requests.token_show_first");
57
    }
58
59
    /**
60
     * @param $text
61
     *
62
     * @return  string
63
     */
64
    protected function set($text)
65
    {
66
        return $this->output = $text;
67
    }
68
69
    /**
70
     * @return  string
71
     */
72
    public function get()
73
    {
74
        return $this->output;
75
    }
76
77
    /**
78
     * @void
79
     */
80
    public function clear()
81
    {
82
        $this->set('');
83
    }
84
85
    /**
86
     * Add header
87
     *
88
     * @param $name
89
     */
90
    public function header($name)
91
    {
92
        $this->append("$name: \n");
93
    }
94
95
    /**
96
     * Add line to indicate new request
97
     *
98
     * @void
99
     */
100
    public function newRequest()
101
    {
102
        $this->append("----------------- NEW REQUEST -----------------");
103
    }
104
105
    /**
106
     * Add empty line
107
     *
108
     * @void
109
     */
110
    public function spaceLine()
111
    {
112
        $this->append("\n \n");
113
    }
114
115
    /**
116
     * @void
117
     */
118
    public function endpoint()
119
    {
120
        $this->append(" * Endpoint: " . $this->request->fullUrl() . "\n");
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method fullUrl() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Illuminate\Foundation\Http\FormRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
121
        $this->append(" * Method: " . $this->request->getMethod() . "\n");
122
    }
123
124
    /**
125
     * @void
126
     */
127
    public function version()
128
    {
129
        if (method_exists($this->request, 'version')) {
130
            $this->append(" * Version: " . $this->request->version() . "\n");
0 ignored issues
show
Bug introduced by Mahmoud Zalt
The method version() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean getProtocolVersion()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
131
        }
132
    }
133
134
    /**
135
     * @void
136
     */
137
    public function ip()
138
    {
139
        $this->append(" * IP: " . $this->request->ip() . " (Port: " . $this->request->getPort() . ") \n");
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method ip() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Illuminate\Foundation\Http\FormRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
140
    }
141
142
    /**
143
     * @void
144
     */
145
    public function format()
146
    {
147
        $this->append(" * Format: " . $this->request->format() . "\n");
0 ignored issues
show
Bug introduced by Mahmoud Zalt
The method format() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean getFormat()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
    }
149
150
    /**
151
     * @void
152
     */
153
    public function userInfo()
154
    {
155
        // Auth Header
156
        $authHeader = $this->request->header("Authorization");
0 ignored issues
show
Bug introduced by Mahmoud Zalt
The method header() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean getTrustedHeaderSet()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
157
        // User
158
        $user = $this->request->user() ? "ID: " . $this->request->user()->id . " (Name: " . $this->request->user()->name . ")" : "N/A";
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method user() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Illuminate\Foundation\Http\FormRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
159
        // Browser
160
        $browser = Agent::browser();
161
162
        $this->append(" * Access Token: " . substr($authHeader, 0,
163
                $this->tokenDataCut) . (!is_null($authHeader) ? "..." : "N/A") . "\n");
164
        $this->append(" * User: " . $user . "\n");
165
        $this->append(" * Device: " . Agent::device() . " (Platform: " . Agent::platform() . ") \n");
166
        $this->append(" * Browser: " . $browser . " (Version: " . Agent::version($browser) . ") \n");
167
        $this->append(" * Languages: " . implode(", ", Agent::languages()) . "\n");
168
    }
169
170
    /**
171
     * @void
172
     */
173
    public function requestData()
174
    {
175
        // Request Data
176
        $requestData = $this->request->all() ? http_build_query($this->request->all(), "", " + ") : "N/A";
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method all() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Illuminate\Foundation\Http\FormRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
177
178
        $this->append(" * " . $requestData . "\n");
179
    }
180
181
    /**
182
     * @void
183
     */
184
    public function responseData()
185
    {
186
        // Response Data
187
        $responseContent = ($this->response && method_exists($this->response,
188
                "content")) ? $this->response->content() : "N/A";
0 ignored issues
show
Bug introduced by Mahmoud Zalt
The method content() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean getContent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
189
190
        $this->append(" * " . substr($responseContent, 0, $this->responseDataCut) . "..." . "\n");
191
    }
192
193
    /**
194
     * @param $output
195
     *
196
     * @return  string
197
     */
198
    private function append($output)
199
    {
200
        return $this->output .= $output;
201
    }
202
203
}
204