1

I'm introducing some more coding quality standards and checks via a new project - in particular, the PHP-FIG recommendations.

This project using Zend Framework 2, and I have a fairly simple entry script which passes on to the MVC application:

define("BASE_PATH", (dirname(__DIR__)));

// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
    return false;
}


// Setup autoloading

// Get the autoloader from composer
$loader = include BASE_PATH . '/vendor/autoload.php';

$zf2Path = BASE_PATH . '/vendor/ZF2/library';

// Add the Zend namespaces/paths to the autoloader
$loader->add('Zend', $zf2Path);
$loader->add('ZendXml', $zf2Path);

if (!class_exists('Zend\Loader\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2.');
}


// Run the application!
Zend\Mvc\Application::init(require BASE_PATH . '/config/application.config.php')->run();

When I run this through PHP-CodeSniffer, I get a warning that:

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 11 and the first side effect is on line 17.

Essentially, PSR-1 does not like that I have a constant definition and then a return statement later on.

Is this an instance where it doesn't matter - as long as the rest of the MVC framework and application is okay, then there's no problem?

4

This rule and consequent warning is sensible - I definitely wouldn't like to see class/constant definitions mixed with side-effects in one file.

But in this particular case I wouldn't mind - PHP bootstrap files are usually a bit like this - setting up the environment which sometimes consists of several distinct (and possibly unrelated) steps.

You could declare the constant in the separate file (e.g. "constants.php") which you can then include into the bootstrap file - this should silence the CodeSniffer. Now, does this make your application better? Is the code better readable? I don't think so, at least for this one constant (if there are more, then yes, it makes sense).

Most such rules apply only in "most" cases, each one has some exceptions and I think think this is the case here ...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.