SynchronousClient::getContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File: SynchronousClient.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\ReactPhpPlayground\Standalone\Http;
12
13
use MSlwk\ReactPhpPlayground\Api\Data\HtmlInterface;
14
use MSlwk\ReactPhpPlayground\Api\Data\UrlInterface;
15
use MSlwk\ReactPhpPlayground\Model\Data\Html;
16
use MSlwk\TypeSafeArray\ObjectArray;
17
use MSlwk\TypeSafeArray\ObjectArrayFactory;
18
19
/**
20
 * Class SynchronousClient
21
 * @package MSlwk\ReactPhpPlayground\Standalone\Http
22
 */
23
class SynchronousClient implements ClientInterface
24
{
25
    /**
26
     * @var ObjectArrayFactory
27
     */
28
    private $objectArrayFactory;
29
30
    /**
31
     * SynchronousClient constructor.
32
     * @param ObjectArrayFactory $objectArrayFactory
33
     */
34
    public function __construct(ObjectArrayFactory $objectArrayFactory)
35
    {
36
        $this->objectArrayFactory = $objectArrayFactory;
37
    }
38
39
    /**
40
     * @param ObjectArray $urls
41
     * @return ObjectArray
42
     */
43 View Code Duplication
    public function getContent(ObjectArray $urls): ObjectArray
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $htmlObjectArray = $this->objectArrayFactory->create(HtmlInterface::class);
46
        /** @var UrlInterface $url */
47
        foreach ($urls as $url) {
48
            $html = $this->fileGetContentsWrapper($url);
49
            $htmlObjectArray->add(new Html($html));
0 ignored issues
show
Bug introduced by
It seems like $html defined by $this->fileGetContentsWrapper($url) on line 48 can also be of type boolean; however, MSlwk\ReactPhpPlayground...ata\Html::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
        }
51
        return $htmlObjectArray;
52
    }
53
54
    /**
55
     * @codeCoverageIgnore
56
     * @param UrlInterface $url
57
     * @return bool|string
58
     */
59
    protected function fileGetContentsWrapper(UrlInterface $url)
60
    {
61
        $html = file_get_contents($url->getUrl());
62
        return $html;
63
    }
64
}
65