Failed Conditions
Pull Request — master (#75)
by Malte
03:57
created

PhantomJsDriverFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 14.29%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 51
ccs 2
cts 14
cp 0.1429
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriverName() 0 4 1
A getDriverDefaults() 0 12 1
A createDriver() 0 11 2
1
<?php
2
/**
3
 * This file is part of the phpunit-mink library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/aik099/phpunit-mink
9
 */
10
11
12
namespace aik099\PHPUnit\MinkDriver;
13
14
15
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16
use Behat\Mink\Driver\DriverInterface;
17
18
class PhantomJsDriverFactory implements IMinkDriverFactory
19
{
20
21
	/**
22
	 * Returns driver name, that can be used in browser configuration.
23
	 *
24
	 * @return string
25
	 */
26 8
	public function getDriverName()
27
	{
28 8
		return 'phantomjs';
29
	}
30
31
	/**
32
	 * Returns default values for browser configuration.
33
	 *
34
	 * @return array
35
	 */
36
	public function getDriverDefaults()
37
	{
38
		return array(
39
			'host' => 'localhost',
40
			'driver' => 'phantomjs',
41
			'driverOptions' => array(
42
				'api_endpoint' => '/api'
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: '/api'
Loading history...
43
			),
44
			'browserName' => 'phantomjs',
45
			'port' => 8510
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: 8510
Loading history...
46
		);
47
	}
48
49
	/**
50
	 * Returns a new driver instance according to the browser configuration.
51
	 *
52
	 * @param BrowserConfiguration $browser The browser configuration.
53
	 *
54
	 * @return DriverInterface
55
	 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
56
	public function createDriver(BrowserConfiguration $browser)
57
	{
58
		if ( !class_exists('Zumba\Mink\Driver\PhantomJSDriver') ) {
59
			throw new \RuntimeException(
60
				'Install PhantomJSDriver in order to use phantomjs driver.'
61
			);
62
		}
63
64
		$driverOptions = $browser->getDriverOptions();
0 ignored issues
show
introduced by
Variable "driverOptions" is not in valid snake caps format
Loading history...
65
		return new \Zumba\Mink\Driver\PhantomJSDriver(sprintf("http://%s:%s%s", $browser->getHost(), $browser->getPort(), $driverOptions['api_endpoint']));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zumba\Mink\D...ions['api_endpoint'])); (Zumba\Mink\Driver\PhantomJSDriver) is incompatible with the return type declared by the interface aik099\PHPUnit\MinkDrive...erFactory::createDriver of type Behat\Mink\Driver\DriverInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
introduced by
Expected 1 blank line before return statement; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal http://%s:%s%s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
introduced by
Variable "driverOptions" is not in valid snake caps format
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 140 characters; contains 155 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
66
	}
67
68
}
69