Issues (40)

php-src/Mapping/DataUrlMapper.php (2 issues)

1
<?php
2
3
namespace kalanis\Restful\Mapping;
4
5
6
use kalanis\Restful\Exceptions\InvalidArgumentException;
7
use kalanis\Restful\Mapping\Exceptions\MappingException;
8
use kalanis\Restful\Resource\Media;
9
use Nette\Utils\Strings;
10
11
12
/**
13
 * DataUrlMapper - encode or decode base64 file
14
 * @package kalanis\Restful\Mapping
15
 */
16 1
class DataUrlMapper implements IMapper
17
{
18
19
    /**
20
     * Create DATA URL from file
21
     * @param string|object|iterable<string|int, mixed> $data
22
     * @param bool $prettyPrint
23
     * @throws InvalidArgumentException
24
     * @return string
25
     *
26
     */
27
    public function stringify(iterable|string|object $data, bool $prettyPrint = true): string
28
    {
29 1
        if (!$data instanceof Media) {
30
            throw new InvalidArgumentException(
31
                'DataUrlMapper expects object of type Media, ' . (gettype($data)) . ' given'
32
            );
33
        }
34 1
        return sprintf('data:%s;base64,%s', $data->getContentType(), base64_encode($data->getContent()));
35
    }
36
37
    /**
38
     * Convert client request data to array or traversable
39
     * @param mixed $data
40
     * @throws MappingException
41
     * @return object
42
     */
43
    public function parse(mixed $data): object
44
    {
45 1
        $matches = Strings::match(strval($data), "@^data:([\w/]+?);(\w+?),(.*)$@si");
0 ignored issues
show
Are you sure the assignment to $matches is correct as Nette\Utils\Strings::mat.../]+?);(\w+?),(.*)$@si') targeting Nette\Utils\Strings::match() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46 1
        if (!$matches) {
0 ignored issues
show
$matches is of type null, thus it always evaluated to false.
Loading history...
47
            throw new MappingException('Given data URL is invalid.');
48
        }
49
50 1
        return new Media(base64_decode((string) $matches[3]), $matches[1]);
51
    }
52
}
53