Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.2

Bug #19561 The PSR-2 reports errors for require_once statements.
Submitted: 2012-08-11 22:44 UTC
From: hinikato Assigned: squiz
Status: Bogus Package: PHP_CodeSniffer (version SVN)
PHP Version: 5.4.5 OS: Windows 7
Roadmaps: (Not assigned)    
Subscription  


 [2012-08-11 22:44 UTC] hinikato (Hinikato Dubrai)
Description: ------------ The phpcs reports errors if file contains require_once statement. In some cases this statement is necessary and I have not found any mentions in PSR-2 that require_once is should be avoided to use. Test script: --------------- <?php namespace MyakTest\Plugin; use Myak\Test\TestCase; require_once __DIR__ . '/_files/PluginBrokerTest/FormPlugin.php'; require_once __DIR__ . '/_files/PluginBrokerTest/MyPluginBroker1.php'; require_once __DIR__ . '/_files/PluginBrokerTest/MyPluginBroker2.php'; class PluginBrokerTest extends TestCase { public function setUp() { $this->broker = new PluginBrokerTest\MyPluginBroker1(); } public function testInstancesUsesOwnMap() { $pluginBroker1 = new PluginBrokerTest\MyPluginBroker1(); $pluginBroker2 = new PluginBrokerTest\MyPluginBroker2(); $this->assertEquals(array('my-plugin1' => 'MyPlugin1'), $pluginBroker1->map); $this->assertEquals(array('my-plugin2' => 'MyPlugin2'), $pluginBroker2->map); } public function testCanUsePlugin() { $plugin = new PluginBrokerTest\FormPlugin(); $this->broker->register($plugin); $form = $this->broker->form('foo', 1, 2); $this->assertInstanceOf('\\' . __CLASS__ . '\\FormPlugin', $form); $this->assertEquals(array('foo', 1, 2), $form->args); } } Expected result: ---------------- No errors. Actual result: -------------- FILE: ...me\project\myak\framework\src\test\MyakTest\Plugin\PluginBrokerTest.php -------------------------------------------------------------------------------- FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 1 | WARNING | 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 10 and the first side effect is | | on line 6. --------------------------------------------------------------------------------

Comments

 [2012-08-13 03:16 UTC] squiz (Greg Sherwood)
-Status: Open +Status: Bogus -Assigned To: +Assigned To: squiz
PSR-2 includes all of PSR-1: https://github.com/php-fig/fig- standards/blob/master/accepted/PSR-1-basic-coding-standard.md PSR-1 includes a part about side effects in files: ----------- 2.3. Side Effects 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 phrase "side effects" means execution of logic not directly related to declaring classes, functions, constants, etc., merely from including the file. "Side effects" include but are not limited to: generating output, explicit use of require or include, connecting to external services, modifying ini settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file, and so on. ----------- So I think PHP_CodeSniffer is right here. You can contribute to the discussion on the google group if you'd like to get confirmation. I started a specific discussion for this part of the standard: https://groups.google.com/forum/#!topic/php- fig/ZkHTOC2wbdU%5B1-25%5D
 [2012-08-14 12:02 UTC] hinikato (Hinikato Dubrai)
squiz, does this mean that using of require_once in class is wrong? If no could you provide working example that passes phpcs checking and uses require_once in class?
 [2012-08-14 12:07 UTC] squiz (Greg Sherwood)
> does this mean that using of require_once in class is wrong? This isn't a question I can answer for you. I did not write the PSR standards. In fact, I had absolutely no involvement in them at all. I just implemented them inside PHPCS as included standards. I also don't use them for anything, so I have no idea how hard they are to adhere to. If you want to ask questions about PSR, you'll need to post in the mailing list for the PHP-FIG group. The list is here: https://groups.google.com/forum/#!forum/php-fig
 [2012-08-14 12:17 UTC] hinikato (Hinikato Dubrai)
@squiz, it clarified the current situation and logic, thanks.
 [2017-10-19 21:48 UTC] bdsl (Barney Laurance)
One simple way to make this file PSR-2 compliant is to move the require_once statements to inside the setUp() function. The files will then be loaded before the first test runs, but not immediately after this file is loaded.