Example #1
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $lines = $file->getLines();
     $severityError = Violation::SEVERITY_ERROR;
     $severityWarning = Violation::SEVERITY_WARNING;
     $severityInfo = Violation::SEVERITY_INFO;
     if (count($lines) > 0) {
         $this->setLimit($severityError, $config->get('error_limit', $this->limits[$severityError]));
         $this->setLimit($severityWarning, $config->get('warning_limit', $this->limits[$severityWarning]));
         $this->setLimit($severityInfo, $config->get('info_limit', $this->limits[$severityInfo]));
         $this->setTabExpand($config->get('tab_expand', $this->tabExpand));
         foreach ($lines as $line => $data) {
             $lineLength = iconv_strlen(str_replace("\t", str_repeat(' ', $this->tabExpand), rtrim($data, "\r\n")), $file->getEncoding());
             $severity = null;
             foreach (Violation::getSeverities() as $severity) {
                 if (!isset($this->limits[$severity]) || $this->limits[$severity] === null) {
                     continue;
                 }
                 if ($lineLength <= $this->limits[$severity]) {
                     continue;
                 }
                 $this->addViolation($file, $line, 0, sprintf('Line is too long. [%d/%d]', $lineLength, $this->limits[$severity]), $severity);
             }
         }
     }
 }
Example #2
0
 /**
  * Runs checks on the file.
  *
  * @param \HippoPHP\Hippo\CheckContext  $checkContext
  * @param \HippoPHP\Hippo\Config\Config $config
  *
  * @return \HippoPHP\Hippo\CheckResult
  */
 public function checkFile(CheckContext $checkContext, Config $config)
 {
     $this->checkResult = new CheckResult();
     $this->checkResult->setFile($checkContext->getFile());
     $this->checkFileInternal($checkContext, $config);
     return $this->checkResult;
 }
Example #3
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param \HippoPHP\Hippo\CheckContext  $checkContext
  * @param \HippoPHP\Hippo\Config\Config $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $this->setIndentStyle($config->get('style', $this->indentStyle));
     $this->setIndentCount($config->get('count', $this->indentCount));
     $indentation = $this->getBaseIndentation();
     $lines = $this->getLines($checkContext->getTokenList());
     $level = 0;
     foreach ($lines as $lineNumber => $line) {
         $actualIndentation = '';
         if (count($line) > 0) {
             if ($line[0]->isType(T_WHITESPACE)) {
                 $actualIndentation = $line[0]->getContent();
             }
         }
         foreach ($line as $token) {
             $content = $token->getContent();
             if ($content === '}' || $content === ')' || $content === ']') {
                 $level--;
             }
         }
         $expectedIndentation = $level > 0 ? str_repeat($indentation, $level) : '';
         if ($expectedIndentation !== $actualIndentation) {
             $this->addViolation($file, $lineNumber, count($line) > 0 ? $line[0]->getColumn() + strlen($line[0]->getContent()) : 1, sprintf('Unexpected indentation (expected: %s, actual: %s)', $this->escape($expectedIndentation), $this->escape($actualIndentation)), Violation::SEVERITY_WARNING);
         }
         foreach ($line as $token) {
             $content = $token->getContent();
             if ($content === '{' || $content === '(' || $content === '[') {
                 $level++;
             }
         }
     }
 }
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     $firstToken = $tokens->rewind()->current();
     if (!$firstToken->isType(T_OPEN_TAG)) {
         $this->addViolation($file, 1, 1, 'Files must begin with the PHP open tag.');
     }
 }
Example #5
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $parser = new Parser(new Emulative());
     try {
         $parser->parse($file->getSource());
     } catch (PhpParserError $e) {
         $this->addViolation($file, $e->getStartLine(), 0, $e->getRawMessage(), Violation::SEVERITY_ERROR);
     }
 }
Example #6
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     $endToken = $tokens->end()->current();
     if (count($file) > 0) {
         if ($config->get('endwith') && !$endToken->isType(T_CLOSE_TAG)) {
             $this->addViolation($file, $endToken->getLine(), 0, 'Files must end with a closing tag.');
         } elseif (!$config->get('endwith') && $endToken->isType(T_CLOSE_TAG)) {
             $this->addViolation($file, $endToken->getLine(), 0, 'Files must not end with a closing tag.');
         }
     }
 }
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $lines = $file->getLines();
     foreach ($lines as $lineNo => $line) {
         if (trim($line) === '') {
             continue;
         }
         $line = rtrim($line, "\r\n");
         if ($line !== rtrim($line)) {
             $this->addViolation($file, $lineNo, 0, 'Excess trailing spaces at end of line.', Violation::SEVERITY_INFO);
         }
     }
 }
Example #8
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $token = $tokens->seekToType($this->tokens)->current();
             $using = $token->getContent();
             $should = $this->useLookup[$token->getType()];
             $this->addViolation($file, trim($token->getLine()), trim($token->getColumn()), sprintf('Use bitwise condition %s instead of %s', $using, $should));
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #9
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $token = $tokens->seekToType(T_COMMENT)->current();
             if (strpos($token->getContent(), '#') === 0) {
                 $this->addViolation($file, $token->getLine(), $token->getColumn(), 'Avoid using bash style comments.', Violation::SEVERITY_ERROR);
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #10
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType([T_IS_EQUAL, T_IS_NOT_EQUAL]);
             $token = $tokens->current();
             if (in_array($token->getContent(), $this->sources)) {
                 $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Avoid the use of the non-strict operator `%s`.', $token->getContent()), Violation::SEVERITY_WARNING);
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #11
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType(T_ELSE)->skipToNextNonWhitespace();
             if ($tokens->current()->isType(T_IF)) {
                 $token = $tokens->current();
                 $this->addViolation($file, $token->getLine(), $token->getColumn(), 'Use `elseif` rather than `else if`');
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #12
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $token = $tokens->seekToType(T_STRING)->current();
             $tokenContent = $token->getContent();
             $lowerContent = strtolower($tokenContent);
             if ($token->isNativeConstant()) {
                 if ($tokenContent !== $lowerContent) {
                     $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('`%s` should be in lowercase.', $tokenContent), Violation::SEVERITY_INFO);
                 }
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #13
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType(T_USE)->skipToNextNonWhitespace()->current();
             if ($tokens->current()->getContent() !== '(') {
                 // Now if the next token does not equal T_NS_SEPARATOR we are not fully qualified.
                 if (!$tokens->current()->isType(T_NS_SEPARATOR)) {
                     $token = $tokens->current();
                     $this->addViolation($file, $token->getLine(), $token->getColumn(), 'Use fully qualified namespaces.');
                 }
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     $this->setPattern($config->get('pattern', $this->pattern));
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType(T_PRIVATE)->skipToNextNonWhitespace();
             $token = $tokens->current();
             if ($token->isType(T_VARIABLE)) {
                 if (!preg_match($this->pattern, $token->getContent())) {
                     $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Private variable `%s` should follow a `%s` pattern', $token->getContent(), addslashes($this->pattern)), Violation::SEVERITY_ERROR);
                 }
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #15
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     if (!function_exists('mb_detect_encoding')) {
         $this->addViolation($file, 0, 0, 'PHP MB extension is disabled. Cannot detect file encoding.', Violation::SEVERITY_WARNING);
         return;
     }
     $this->setEncodingType($config->get('encoding', $this->encoding));
     $this->setWithBOM($config->get('bom', $this->bom));
     $encoding = mb_detect_encoding($file->getSource(), $this->encoding, true);
     if ($encoding !== $this->encoding) {
         $this->addViolation($file, 0, 0, sprintf('File encoding should be %s. Currently using %s', $this->encoding, $encoding), Violation::SEVERITY_INFO);
         // Are we checking for BOM too?
         if ($this->bom) {
             if (false === strpos($file->getSource, self::BOM)) {
                 $this->addViolation($file, 0, 0, 'Files should be saved with BOM.', Violation::SEVERITY_INFO);
             }
         }
     }
 }
Example #16
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     $this->setQuoteStyle($config->get('style', $this->style));
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType(T_CONSTANT_ENCAPSED_STRING);
             $token = $tokens->current();
             if ($this->isBadStyle($token)) {
                 // Work out what style we shouldn't be using.
                 $styles = $this->styles;
                 unset($styles[$this->style]);
                 $badStyle = array_keys($styles)[0];
                 $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Prefer %s quotes to %s', addslashes($this->style), $badStyle), Violation::SEVERITY_INFO);
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }
Example #17
0
 /**
  * checkFileInternal(): defined by AbstractCheck.
  *
  * @see AbstractCheck::checkFileInternal()
  *
  * @param CheckContext $checkContext
  * @param Config       $config
  */
 protected function checkFileInternal(CheckContext $checkContext, Config $config)
 {
     $file = $checkContext->getFile();
     $tokens = $checkContext->getTokenList();
     try {
         do {
             // Jump us to the next token we want to check.
             $tokens->seekToType(T_VARIABLE);
             $token = $tokens->current();
             // If the content !== $ then go back a token, is that $?
             if ($token->getContent() !== '$') {
                 $prevTokenList = clone $tokens;
                 $prevToken = $prevTokenList->prev()->current();
                 if ($prevToken->getContent() === '$') {
                     $this->addViolation($file, $token->getLine(), $token->getColumn(), 'Do not use variable variables.', Violation::SEVERITY_ERROR);
                 }
             }
         } while ($tokens->valid());
     } catch (\HippoPHP\Tokenizer\Exception\OutOfBoundsException $e) {
         // Ignore the exception, we're at the end of the file.
     }
 }