for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace DICIT;
class ReferenceResolver
{
const CONTAINER_REGEXP = '`^\$container$`i';
const ENVIRONMENT_REGEXP = '`^\$env\.(.*)$`i';
const CONSTANT_REGEXP = '`^\$const\.(.*)$`i';
/**
*
* @var Container
*/
private $container;
public function __construct(Container $container)
$this->container = $container;
}
* Return the resolved value of the given reference
* @param mixed $reference
* @return mixed
public function resolve($reference)
if (!is_string($reference)) {
return $reference;
$prefix = substr($reference, 0, 1);
switch (1) {
case $prefix === '@' : return $this->container->get(substr($reference, 1));
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
switch ($selector) { case "A": //right doSomething(); break; case "B" : //wrong doSomethingElse(); break; }
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
switch ($expr) { case "A": doSomething(); //right break; case "B": doSomethingElse(); //wrong break;
As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.
break
switch ($expr) { case "A": doSomething(); break; //wrong case "B": doSomething(); break; //right case "C:": doSomething(); return true; //right }
case $prefix === '%' : return $this->container->getParameter(substr($reference, 1));
case preg_match(static::CONTAINER_REGEXP, $reference, $matches) : return $this->container;
case preg_match(static::ENVIRONMENT_REGEXP, $reference, $matches) : return getenv($matches[1]);
$matches
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
function someFunction() { $x = 5; echo $x; }
The above code is perfectly fine. Now imagine that we re-order the statements:
function someFunction() { echo $x; $x = 5; }
In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.
$x
case preg_match(static::CONSTANT_REGEXP, $reference, $matches) : return constant($matches[1]);
default : return $reference;
As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.
switch ($expr) { default : //wrong doSomething(); break; } switch ($expr) { default: //right doSomething(); break; }
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
switch ($expr) { default: doSomething(); //right break; } switch ($expr) { default: doSomething(); //wrong break; }
public function resolveMany(array $references)
$convertedParameters = array();
foreach ($references as $reference) {
$convertedValue = $this->resolve($reference);
$convertedParameters[] = $convertedValue;
return $convertedParameters;
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.