Failed Conditions
Push — master ( 7014aa...7fc00d )
by Alexander
04:08
created

ProcessFactory::createCommandProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy 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/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Process;
12
13
14
use Symfony\Component\Process\PhpExecutableFinder;
15
use Symfony\Component\Process\Process;
16
use Symfony\Component\Process\ProcessBuilder;
17
18
class ProcessFactory implements IProcessFactory
19
{
20
21
	/**
22
	 * Creates new Symfony process with given arguments.
23
	 *
24
	 * @param string       $commandline  The command line to run.
25
	 * @param integer|null $idle_timeout Idle timeout.
26
	 *
27
	 * @return Process
28
	 */
29 1
	public function createProcess(
30
		$commandline,
31
		$idle_timeout = null
32
	) {
33 1
		$process = new Process($commandline);
34 1
		$process->setTimeout(null);
35 1
		$process->setIdleTimeout($idle_timeout);
36
37 1
		return $process;
38
	}
39
40
	/**
41
	 * Creates new Symfony PHP process with given arguments.
42
	 *
43
	 * @param string $command   Command.
44
	 * @param array  $arguments Arguments.
45
	 *
46
	 * @return Process
47
	 * @throws \RuntimeException When PHP executable can't be found.
48
	 */
49
	public function createCommandProcess($command, array $arguments = array())
1 ignored issue
show
Coding Style introduced by
createCommandProcess uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50
	{
51
		$php_executable_finder = new PhpExecutableFinder();
52
		$php_executable = $php_executable_finder->find();
53
54
		if ( !$php_executable ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $php_executable of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
			throw new \RuntimeException('The PHP executable cannot be found.');
56
		}
57
58
		array_unshift($arguments, $php_executable, $_SERVER['argv'][0], $command);
59
60
		return ProcessBuilder::create($arguments)->getProcess();
61
	}
62
63
}
64