Completed
Push — master ( f74871...f70479 )
by Elf
02:21
created

usage.php ➔ dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 45 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use ElfSundae\HttpClient;
4
5
require __DIR__.'/../vendor/autoload.php';
6
7
$client = new HttpClient('https://httpbin.org');
8
9
dump(__LINE__, $client->request('http://icanhazip.com')->getContent());
10
dump(__LINE__, (new HttpClient)->fetchContent('http://icanhazip.com'));
11
12
dump(__LINE__, $client->request('/ip')->getJson());
13
dump(__LINE__, $client->fetchJson('/ip'));
14
15
dump(__LINE__, $client->header('X-FOO', 'bar')->fetchJson('/headers'));
16
17
try {
18
    $client->withExceptions(true)->fetchContent('/status/418');
19
} catch (\Exception $e) {
20
    dump(__LINE__, $e->getCode(), $e->getMessage());
21
}
22
23
24
dump(__LINE__,
25
    (new HttpClient)
0 ignored issues
show
Documentation Bug introduced by
The method formParams does not exist on object<ElfSundae\HttpClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
26
    ->formParams(['user' => 'Elf Sundae'])
27
    ->fetchJson('https://httpbin.org/post', 'POST')
28
);
29
30
dump(__LINE__, $client->saveTo(__DIR__.'/image.png')->request('/image/png')->getStatusCode());
31
32
// Options
33
$client->option('cookies', new \GuzzleHttp\Cookie\CookieJar())
0 ignored issues
show
Documentation Bug introduced by
The method auth does not exist on object<ElfSundae\HttpClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34
    ->auth(['username', 'password'])
35
    ->cert('/path/server.pem')
36
    ->debug(true)
37
    ->httpErrors(false)
38
    ->progress(function () {})
39
    ->verify(false)
40
    ->version(2)
41
    ->acceptJson();
42
dump(__LINE__, $client->getOptions());
43
44
45
function dump($line, ...$data)
46
{
47
    usleep(300000);
48
    echo "====================  $line  ===========================".PHP_EOL;
49
    var_dump(...$data);
0 ignored issues
show
Security Debugging Code introduced by
var_dump(...$data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
50
}
51