Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error in JS file can cause undefined index for parenthesis_closer #1025

Closed
jnwarp opened this issue May 29, 2016 · 3 comments
Closed

Comments

@jnwarp
Copy link

jnwarp commented May 29, 2016

When running code checks on my repository, a PHP Notice is produced.

PHP Notice: Undefined index: parenthesis_closer in C:\Users\ajame\Documents\runner-test\vendor\squizlabs\php_codesniffer\CodeSniffer\File.php on line 3321

The error is found in this function:

public function findPrevious(
        $types,
        $start,
        $end=null,
        $exclude=false,
        $value=null,
        $local=false
    ) {
        $types = (array) $types;

        if ($end === null) {
            $end = 0;
        }

        for ($i = $start; $i >= $end; $i--) {
            $found = (bool) $exclude;
            foreach ($types as $type) {
                if ($this->_tokens[$i]['code'] === $type) {
                    $found = !$exclude;
                    break;
                }
            }

            if ($found === true) {
                if ($value === null) {
                    return $i;
                } else if ($this->_tokens[$i]['content'] === $value) {
                    return $i;
                }
            }

            if ($local === true) {
                if (isset($this->_tokens[$i]['scope_opener']) === true
                    && $i === $this->_tokens[$i]['scope_closer']
                ) {
                    $i = $this->_tokens[$i]['scope_opener'];
                } else if (isset($this->_tokens[$i]['bracket_opener']) === true
                    && $i === $this->_tokens[$i]['bracket_closer']
                ) {
                    $i = $this->_tokens[$i]['bracket_opener'];
                } else if (isset($this->_tokens[$i]['parenthesis_opener']) === true
                    && $i === $this->_tokens[$i]['parenthesis_closer']
                ) {
                    $i = $this->_tokens[$i]['parenthesis_opener'];
                } else if ($this->_tokens[$i]['code'] === T_SEMICOLON) {
                    break;
                }
            }
        }//end for

        return false;

    }//end findPrevious()

The error is produced by this invalid javascript file:

$(document).ready(function () {
  console.log('hello world');
}

Notice the missing ); at the end of the $(document).ready.

@aik099
Copy link
Contributor

aik099 commented May 29, 2016

Which version of PHP_CodeSniffer are you using? Does the same happen in master as well?

@jnwarp
Copy link
Author

jnwarp commented May 29, 2016

I am using version 2.6.0 installed through composer. If I set composer to download the version from the master branch, the error persists.

PHP Notice: Undefined index: parenthesis_closer in C:\Users\ajame\Documents\gitlab-runner-test\vendor\squizlabs\php_codesniffer\CodeSniffer\File.php on line 3310

@gsherwood gsherwood changed the title Undefined index: parenthesis_closer Syntax error in JS file can cause undefined index for parenthesis_closer May 30, 2016
gsherwood added a commit that referenced this issue May 30, 2016
@gsherwood
Copy link
Member

This has been fixed now. Thanks for reporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants