PHP_Codesniffer says “Undefined offset: 2 in /PHP_Codesniffer/src/Files/File.php on line 863”

I am working on an analytics plugin for Shopware to expand the statistics section. Everything works as expected, but when I wanted to commit the code for the controller I got the above mentioned error.

I can’t seem to find the problem and would be thankful for any input.

<?php

class Shopware_Controllers_Backend_CustomStatistics extends Shopware_Controllers_Backend_ExtJs
{
  public function getPreorderSubsAction() {
    $connection = $this->container->get('dbal_connection');
    $query = $connection->createQueryBuilder();
    $query->select([
      'ps.abo',
      'smao.name',
      'ROUND(SUM(ps.preordered * ps.unit_price),2) AS preorder_value'
      ])
      ->from('vw_PreorderSubs', 'ps')
      ->join('ps','salt_model_abo', 'smao','ps.abo = smao.id')
      ->groupBy('ps.abo');

    $data = $query->execute()->fetchAll();

    $this->View()->assign([
        'success' => true,
        'data' => $data,
        'count' => count($data)
    ]);
  }
}

The corresponding code from the codesniffer:

// Check if this line is ignoring all message codes.
        if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) {
            return false;
        }

        // Work out which sniff generated the message.
        $parts = explode('.', $code);
        if ($parts[0] === 'Internal') {
            // An internal message.
            $listenerCode = UtilCommon::getSniffCode($this->activeListener);
            $sniffCode    = $code;
            $checkCodes   = [$sniffCode];
        } else {
            if ($parts[0] !== $code) {
                // The full message code has been passed in.
                $sniffCode    = $code;
                $listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.'));
            } else {
                $listenerCode = UtilCommon::getSniffCode($this->activeListener);
                $sniffCode    = $listenerCode.'.'.$code;
                $parts        = explode('.', $sniffCode);
            }

            $checkCodes = [
                $sniffCode,
                $parts[0].'.'.$parts[1].'.'.$parts[2],
                $parts[0].'.'.$parts[1],
                $parts[0],
            ];
        }//end if

Line 863 is

$parts[0].'.'.$parts[1].'.'.$parts[2],

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It appears that a misplaced { was the cause for the error:

<?php

class Shopware_Controllers_Backend_CustomStatistics extends Shopware_Controllers_Backend_ExtJs {
  /**
   * calls the getPreorderSubsAction function that connects with the database and
   * delivers the content for the new statistics table
   *
   * @return void
   */
  public function getPreorderSubsAction() {
    $connection = $this->container->get('dbal_connection');
    $query = $connection->createQueryBuilder();
    $query->select([
      'ps.abo',
      'smao.name',
      'ROUND(SUM(ps.preordered * ps.unit_price),2) AS preorder_value'
      ])
      ->from('vw_PreorderSubs', 'ps')
      ->join('ps','salt_model_abo', 'smao','ps.abo = smao.id')
      ->groupBy('ps.abo');

    $data = $query->execute()->fetchAll();

    $this->View()->assign([
        'success' => true,
        'data' => $data,
        'count' => count($data)
    ]);
  }
}

This works now.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x