Search code examples
phpshopwarephpcodesniffer

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 = Util\Common::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 = Util\Common::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],

Solution

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