slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 171 forks source link

Missing index breaks phpcbf run #799

Closed mimmi20 closed 4 years ago

mimmi20 commented 4 years ago

When running a fix (phpcbf) I got this error:

PHP Fatal error:  Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: scope_closer in /home/developer/projects/phing/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 195 in /home/developer/projects/phing/vendor/squizlabs/php_codesniffer/src/Runner.php:606
Stack trace:
#0 /home/developer/projects/phing/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php(195): PHP_CodeSniffer\Runner->handleErrors(8, 'Undefined index...', '/home/developer...', 195, Array)
#1 /home/developer/projects/phing/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php(137): SlevomatCodingStandard\Helpers\UseStatementHelper::getUseStatementPointers(Object(PHP_CodeSniffer\Files\LocalFile), 0)
#2 /home/developer/projects/phing/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php(99): SlevomatCodingStandard\Helpers\UseStatementHelper::getFileUseStatements(Object(PHP_CodeSniffer\Files\LocalFile))
#3 / in /home/developer/projects/phing/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

used version: 6.0.1 (via Doctrine coding standard) PHP version: 7.2.24 OS: Debian Linux

used configuration:

<?xml version="1.0"?>
<ruleset
    name="PHPCS Coding Standards for phing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
    <description>Check the code of phing in phing coding-standards.</description>

    <arg name="basepath" value="."/>
    <arg name="extensions" value="php"/>

    <!-- Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting. -->
    <rule ref="Generic.WhiteSpace.ScopeIndent"/>
    <rule ref="Generic.WhiteSpace.DisallowTabIndent"/>

    <!-- PHP code MUST use only UTF-8 without BOM. -->
    <rule ref="Generic.Files.ByteOrderMark"/>

    <!-- Never use shorthand PHP start tags. Always use full PHP tags. -->
    <rule ref="Generic.PHP.DisallowShortOpenTag"/>
    <rule ref="Generic.PHP.DisallowAlternativePHPTags"/>

    <!-- PHP keywords MUST be in lower case. -->
    <rule ref="Generic.PHP.LowerCaseKeyword"/>

    <!-- The PHP constants true, false, and null MUST be in lower case. -->
    <rule ref="Generic.PHP.LowerCaseConstant"/>

    <!-- PHP files should contain an empty line at the end of the file -->
    <rule ref="Generic.Files.EndFileNewline"/>

    <!-- Allow only unix file endings (newlines) -->
    <rule ref="Generic.Files.LineEndings">
        <properties>
            <property name="eolChar" value="\n"/>
        </properties>
    </rule>

    <!-- Braces should always be used, even when they are not required. -->
    <rule ref="Generic.ControlStructures.InlineControlStructure"/>

    <!-- Rule: In general, readability is more important than cleverness or brevity. -->
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>

    <rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
    <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>

    <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman">
        <exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine"/>
    </rule>

    <rule ref="PSR12">
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
        <exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
        <exclude name="PSR12.Files.FileHeader.SpacingAfterBlock"/>
        <exclude name="Squiz.Functions.LowercaseFunctionKeywords"/><!-- checked by more generic Generic.PHP.LowerCaseKeyword -->
        <exclude name="Squiz.ControlStructures.LowercaseDeclaration"/><!-- checked by more generic Generic.PHP.LowerCaseKeyword -->
        <exclude name="PSR12.Functions.ReturnTypeDeclaration"/>
        <exclude name="PSR2.ControlStructures.SwitchDeclaration"/><!-- checked by SlevomatCodingStandard.ControlStructures.AbstractControlStructureSpacing -->
    </rule>

    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
        <properties>
            <property name="ignoreBlankLines" value="false"/>
        </properties>
    </rule>

    <rule ref="Doctrine">
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
        <exclude name="PSR2.ControlStructures.ControlStructureSpacing"/><!-- checked by PSR12.ControlStructures.ControlStructureSpacing -->
        <!-- Allow `!` without trailing whitespace -->
        <exclude name="Generic.Formatting.SpaceAfterNot.Incorrect"/>
        <exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden"/>
        <!-- will be fixed in a different branch -->
        <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
        <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
        <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingPropertyTypeHint"/>
        <exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint"/>
        <exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint"/>
        <exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
    </rule>

    <rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
        <properties>
            <property name="spacesCountBeforeColon" value="0"/>
        </properties>
    </rule>

    <rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
        <properties>
            <property name="traversableTypeHints" type="array">
                <element value="Traversable"/>
                <element value="Iterator"/>
                <element value="IteratorAggregate"/>
                <element value="Doctrine\Common\Collections\Collection"/>
            </property>
            <property name="enableObjectTypeHint" value="false"/>
        </properties>
    </rule>
</ruleset>

The content of the current file was:

<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */

/**
 * This is a FileSet with the to specify permissions.
 *
 * Permissions are currently not implemented by PEAR Archive_Tar,
 * but hopefully they will be in the future.
 *
 * @package phing.tasks.ext
 */
class ZipFileSet extends FileSet
{
    private $files = null;

    /**
     *  Get a list of files and directories specified in the fileset.
     *
     * @param bool $includeEmpty
     * @param array ...$options
     *
     * @return array a list of file and directory names, relative to
     *               the baseDir for the project.
     *
     * @throws Exception
     */
    protected function getFiles($includeEmpty = true, ...$options)
    {
        if ($this->files === null) {
            $ds = $this->getDirectoryScanner($this->getProject());
            $this->files = $ds->getIncludedFiles();

            // build a list of directories implicitly added by any of the files
            $implicitDirs = [];
            foreach ($this->files as $file) {
                $implicitDirs[] = dirname($file);
            }

            $incDirs = $ds->getIncludedDirectories();

            // we'll need to add to that list of implicit dirs any directories
            // that contain other *directories* (and not files), since otherwise
            // we get duplicate directories in the resulting tar
            foreach ($incDirs as $dir) {
                foreach ($incDirs as $dircheck) {
                    if (!empty($dir) && $dir == dirname($dircheck)) {
                        $implicitDirs[] = $dir;
                    }
                }
            }

            $implicitDirs = array_unique($implicitDirs);

            $emptyDirectories = [];

            if ($includeEmpty) {
                // Now add any empty dirs (dirs not covered by the implicit dirs)
                // to the files array.

                foreach ($incDirs as $dir) { // we cannot simply use array_diff() since we want to disregard empty/. dirs
                    if ($dir != "" && $dir !== "." && !in_array($dir, $implicitDirs)) {
                        // it's an empty dir, so we'll add it.
                        $emptyDirectories[] = $dir;
                    }
                }
            } // if $includeEmpty

            $this->files = array_merge($implicitDirs, $emptyDirectories, $this->files);
            sort($this->files);
        } // if ($this->files===null)

        return $this->files;
    }
}

This is related to issue #793.

kukulich commented 4 years ago

It's bug in PHPCS itself. Duplicated to: https://github.com/slevomat/coding-standard/issues/254

It looks it's only on PHP version 7.2 and lower.

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.