/** * 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(); $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); } } } }
/** * @param \HippoPHP\Hippo\File $file * * @return \HippoPHP\Hippo\CheckResult[] */ public function checkFile(File $file) { $checkContext = new CheckContext($file); $results = []; foreach ($this->checkRepository->getChecks() as $check) { $branch = $this->config->get($check->getConfigRoot()); if ($branch->get('enabled') === true) { $results[] = $check->checkFile($checkContext, $branch); } } return $results; }
/** * 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(); $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. } }
/** * 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); } } } }
/** * 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. } }