Passed
Pull Request — master (#2)
by Florian
02:55
created

ZendStreamFactory::createStreamFromResource()   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
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Authentication\HttpFactory;
15
16
use Psr\Http\Message\StreamFactoryInterface;
17
use Psr\Http\Message\StreamInterface;
18
use Zend\Diactoros\Stream;
19
20
/**
21
 * Zend Stream Factory
22
 */
23
class ZendStreamFactory implements StreamFactoryInterface
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function createStream(string $content = ''): StreamInterface
29
    {
30
        $resource = fopen('php://memory', 'rw');
31
        fwrite($resource, $content);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        fwrite(/** @scrutinizer ignore-type */ $resource, $content);
Loading history...
32
        rewind($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

32
        rewind(/** @scrutinizer ignore-type */ $resource);
Loading history...
33
34
        return $this->createStreamFromResource($resource);
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
41
    {
42
        $resource = fopen($file, $mode);
43
44
        return $this->createStreamFromResource($resource);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function createStreamFromResource($resource): StreamInterface
51
    {
52
        return new Stream($resource);
53
    }
54
}
55