Passed
Pull Request — 1.x (#23)
by Wouter
02:36
created

HasBrowser::httpBrowser()   B

Complexity

Conditions 10
Paths 42

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 26
c 2
b 0
f 0
nc 42
nop 2
dl 0
loc 51
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\Bundle\FrameworkBundle\Test\WebTestCase;
7
use Zenstruck\Browser\KernelBrowser;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait HasBrowser
13
{
14
    public function browser(array $options = []): KernelBrowser
15
    {
16
        if (!$this instanceof KernelTestCase) {
17
            throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, KernelTestCase::class));
18
        }
19
20
        $class = $_SERVER['KERNEL_BROWSER_CLASS'] ?? KernelBrowser::class;
21
22
        if (!\is_a($class, KernelBrowser::class, true)) {
23
            throw new \RuntimeException(\sprintf('"KERNEL_BROWSER_CLASS" env variable must reference a class that extends %s.', KernelBrowser::class));
24
        }
25
26
        if ($this instanceof WebTestCase) {
27
            static::ensureKernelShutdown();
28
29
            $browser = new $class(static::createClient($options));
30
        } else {
31
            // reboot kernel before starting browser
32
            static::bootKernel($options);
33
34
            if (!static::$container->has('test.client')) {
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...
35
                throw new \RuntimeException('The Symfony test client is not enabled.');
36
            }
37
38
            $browser = new $class(static::$container->get('test.client'));
39
        }
40
41
        BrowserExtension::registerBrowser($browser);
42
43
        return $browser
44
            ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
45
        ;
46
    }
47
}
48