[PEAR-BUG] Bug #19561 [Com]: The PSR-2 reports errors for require_once statements.

[email protected] ("bdsl") Thu, 19 Oct 2017 17:54:15 -0400 (EDT)
Newsgroups php.pear.bugs
Message-ID <[email protected]>
Edit report at http://pear.php.net/bugs/bug.php?id=19561&edit=1

 ID:               19561
 Comment by:       bdsl
 Reported By:      barney at redmagic dot org dot uk
 Summary:          The PSR-2 reports errors for require_once statements.
 Status:           Bogus
 Type:             Bug
 Package:          PHP_CodeSniffer
 Operating System: Windows 7
 Package Version:  SVN
 PHP Version:      5.4.5
 Assigned To:      squiz
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[2012-08-14 08:17:43] hinikato

@squiz, it clarified the current situation and logic, thanks.

------------------------------------------------------------------------

[2012-08-14 08:07:18] squiz

> 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 08:02:14] hinikato

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-12 23:16:38] squiz

<div id="changeset">
<span class="removed">-Status:      Open</span>
<span class="added">+Status:      Bogus</span>
<span class="removed">-Assigned To:</span>
<span class="added">+Assigned To: squiz</span>
</div>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-11 18:44:19] hinikato

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.
--------------------------------------------------------------------------------

------------------------------------------------------------------------


-- 
Edit this bug report at http://pear.php.net/bugs/bug.php?id=19561&edit=1