Failed Conditions
Push — master ( bbfade...32fb37 )
by Florent
02:44
created

StringStream::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace U2FAuthentication\Fido2;
15
16
use CBOR\Stream;
17
18
final class StringStream implements Stream
19
{
20
    private $resource;
21
22
    /**
23
     * StringStream constructor.
24
     *
25
     * @param string $data
26
     */
27
    public function __construct(string $data)
28
    {
29
        $this->resource = fopen('php://memory', 'r+');
30
        fwrite($this->resource, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->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

30
        fwrite(/** @scrutinizer ignore-type */ $this->resource, $data);
Loading history...
31
        rewind($this->resource);
0 ignored issues
show
Bug introduced by
It seems like $this->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

31
        rewind(/** @scrutinizer ignore-type */ $this->resource);
Loading history...
32
    }
33
34
    public function read(int $length): string
35
    {
36
        if (0 === $length) {
37
            return '';
38
        }
39
        $data = fread($this->resource, $length);
0 ignored issues
show
Bug introduced by
It seems like $this->resource can also be of type false; however, parameter $handle of fread() 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

39
        $data = fread(/** @scrutinizer ignore-type */ $this->resource, $length);
Loading history...
40
        if (!is_string($data)) {
0 ignored issues
show
introduced by
The condition is_string($data) is always true.
Loading history...
41
            throw new \InvalidArgumentException('Cannot stream the data');
42
        }
43
        if (mb_strlen($data, '8bit') !== $length) {
44
            throw new \InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d', $length, mb_strlen($data, '8bit')));
45
        }
46
47
        return $data;
48
    }
49
}
50