Passed
Pull Request — 1.x (#23)
by Wouter
01:49
created

HasHttpBrowser::browser()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 58
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 58
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Zenstruck\Browser\Test;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient;
7
use Zenstruck\Browser\HttpBrowser;
8
9
trait HasHttpBrowser
10
{
11
    /** @var HttpBrowserClient[] */
12
    private static array $httpBrowserClients = [];
13
14
    /**
15
     * @internal
16
     * @after
17
     */
18
    final public function _resetHttpBrowserClients(): void
19
    {
20
        self::$httpBrowserClients = [];
21
    }
22
23
    protected function browser(): HttpBrowser
24
    {
25
        $browser = HttpBrowser::create(function() {
26
            $class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
27
28
            if (!\is_a($class, HttpBrowser::class, true)) {
29
                throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
30
            }
31
32
            $baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
33
34
            if (!$baseUri && !$this instanceof PantherTestCase) {
0 ignored issues
show
Bug introduced by
The type Zenstruck\Browser\Test\PantherTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
                throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
36
            }
37
38
            if (!$baseUri) {
39
                self::startWebServer();
40
41
                $baseUri = self::$baseUri;
42
            }
43
44
            // copied from PantherTestCaseTrait::createHttpBrowserClient()
45
            $client = new HttpBrowserClient();
46
            $urlComponents = \parse_url($baseUri);
47
            $host = $urlComponents['host'];
48
49
            if (isset($urlComponents['port'])) {
50
                $host .= ":{$urlComponents['port']}";
51
            }
52
53
            $client->setServerParameter('HTTP_HOST', $host);
54
55
            if ('https' === ($urlComponents['scheme'] ?? 'http')) {
56
                $client->setServerParameter('HTTPS', 'true');
57
            }
58
59
            /** @var HttpBrowser $browser */
60
            $browser = new $class(self::$httpBrowserClients[] = $client);
61
62
            if (!$this instanceof KernelTestCase) {
63
                return $browser;
64
            }
65
66
            if (!static::$booted) {
0 ignored issues
show
Bug introduced by
The property booted is declared protected in Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and cannot be accessed from this context.
Loading history...
67
                static::bootKernel();
68
            }
69
70
            if (static::$container->has('profiler')) {
0 ignored issues
show
Bug introduced by
The property container is declared protected in Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and cannot be accessed from this context.
Loading history...
71
                $browser->setProfiler(static::$container->get('profiler'));
0 ignored issues
show
Bug introduced by
It seems like static::container->get('profiler') can also be of type null; however, parameter $profiler of Zenstruck\Browser\HttpBrowser::setProfiler() does only seem to accept Symfony\Component\HttpKernel\Profiler\Profiler, 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

71
                $browser->setProfiler(/** @scrutinizer ignore-type */ static::$container->get('profiler'));
Loading history...
72
            }
73
74
            return $browser;
75
        });
76
77
        BrowserExtension::registerBrowser($browser);
78
79
        return $browser
80
            ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
81
        ;
82
    }
83
}
84