本文整理汇总了PHP中PHP_CodeSniffer\Files\File类的典型用法代码示例。如果您正苦于以下问题:PHP File类的具体用法?PHP File怎么用?PHP File使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了File类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// We are only interested in the first token in a multi-line string.
if ($tokens[$stackPtr]['code'] === $tokens[$stackPtr - 1]['code']) {
return;
}
$workingString = $tokens[$stackPtr]['content'];
$lastStringToken = $stackPtr;
$i = $stackPtr + 1;
if (isset($tokens[$i]) === true) {
while ($i < $phpcsFile->numTokens && $tokens[$i]['code'] === $tokens[$stackPtr]['code']) {
$workingString .= $tokens[$i]['content'];
$lastStringToken = $i;
$i++;
}
}
// Check if it's a double quoted string.
if (strpos($workingString, '"') === false) {
return;
}
// Make sure it's not a part of a string started in a previous line.
// If it is, then we have already checked it.
if ($workingString[0] !== '"') {
return;
}
// The use of variables in double quoted strings is not allowed.
if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
$stringTokens = token_get_all('<?php ' . $workingString);
foreach ($stringTokens as $token) {
if (is_array($token) === true && $token[0] === T_VARIABLE) {
$error = 'Variable "%s" not allowed in double quoted string; use concatenation instead';
$data = array($token[1]);
$phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data);
}
}
return;
}
//end if
$allowedChars = array('\\0', '\\1', '\\2', '\\3', '\\4', '\\5', '\\6', '\\7', '\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\\b', '\\e', '\\u', '\'');
foreach ($allowedChars as $testChar) {
if (strpos($workingString, $testChar) !== false) {
return;
}
}
$error = 'String %s does not require double quotes; use single quotes instead';
$data = array(str_replace("\n", '\\n', $workingString));
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$innerContent = substr($workingString, 1, -1);
$innerContent = str_replace('\\"', '"', $innerContent);
$phpcsFile->fixer->replaceToken($stackPtr, "'{$innerContent}'");
while ($lastStringToken !== $stackPtr) {
$phpcsFile->fixer->replaceToken($lastStringToken, '');
$lastStringToken--;
}
$phpcsFile->fixer->endChangeset();
}
}
示例2: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$csslintPath = Config::getExecutablePath('csslint');
if ($csslintPath === null) {
return;
}
$fileName = $phpcsFile->getFilename();
$cmd = $csslintPath . ' ' . escapeshellarg($fileName);
exec($cmd, $output, $retval);
if (is_array($output) === false) {
return;
}
$count = count($output);
for ($i = 0; $i < $count; $i++) {
$matches = array();
$numMatches = preg_match('/(error|warning) at line (\\d+)/', $output[$i], $matches);
if ($numMatches === 0) {
continue;
}
$line = (int) $matches[2];
$message = 'csslint says: ' . $output[$i + 1];
// First line is message with error line and error code.
// Second is error message.
// Third is wrong line in file.
// Fourth is empty line.
$i += 4;
$phpcsFile->addWarningOnLine($message, $line, 'ExternalTool');
}
//end for
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
示例3: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$parts = array(0, 0, 0);
$index = 0;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if ($code === T_SEMICOLON) {
++$index;
} else {
if (isset(Tokens::$emptyTokens[$code]) === false) {
++$parts[$index];
}
}
}
if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
$error = 'This FOR loop can be simplified to a WHILE loop';
$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
}
}
示例4: processMemberVar
/**
* Processes class member variables.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
if (empty($memberProps) === true) {
return;
}
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
$scope = $memberProps['scope'];
$scopeSpecified = $memberProps['scope_specified'];
if ($memberProps['scope'] === 'private') {
$isPublic = false;
} else {
$isPublic = true;
}
// If it's a private member, it must have an underscore on the front.
if ($isPublic === false && $memberName[0] !== '_') {
$error = 'Private member variable "%s" must be prefixed with an underscore';
$data = array($memberName);
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
return;
}
// If it's not a private member, it must not have an underscore on the front.
if ($isPublic === true && $scopeSpecified === true && $memberName[0] === '_') {
$error = '%s member variable "%s" must not be prefixed with an underscore';
$data = array(ucfirst($scope), $memberName);
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
return;
}
}
示例5: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
// Probably an interface method.
return;
}
$openBrace = $tokens[$stackPtr]['scope_opener'];
$nextContent = $phpcsFile->findNext(T_WHITESPACE, $openBrace + 1, null, true);
if ($nextContent === $tokens[$stackPtr]['scope_closer']) {
// The next bit of content is the closing brace, so this
// is an empty function and should have a blank line
// between the opening and closing braces.
return;
}
$braceLine = $tokens[$openBrace]['line'];
$nextLine = $tokens[$nextContent]['line'];
$found = $nextLine - $braceLine - 1;
if ($found > 0) {
$error = 'Expected 0 blank lines after opening function brace; %s found';
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $openBrace, 'SpacingAfter', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $openBrace + 1; $i < $nextContent; $i++) {
if ($tokens[$i]['line'] === $nextLine) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($openBrace);
$phpcsFile->fixer->endChangeset();
}
}
}
示例6: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$nestingLevel = 0;
// Find the maximum nesting level of any token in the function.
for ($i = $start + 1; $i < $end; $i++) {
$level = $tokens[$i]['level'];
if ($nestingLevel < $level) {
$nestingLevel = $level;
}
}
// We subtract the nesting level of the function itself.
$nestingLevel = $nestingLevel - $tokens[$stackPtr]['level'] - 1;
if ($nestingLevel > $this->absoluteNestingLevel) {
$error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s';
$data = array($nestingLevel, $this->absoluteNestingLevel);
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
} else {
if ($nestingLevel > $this->nestingLevel) {
$warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function';
$data = array($nestingLevel, $this->nestingLevel);
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}
示例7: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevType = $tokens[$stackPtr - 1]['code'];
if (isset(Tokens::$emptyTokens[$prevType]) === false) {
return;
}
$nonSpace = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 2, null, true);
if ($tokens[$nonSpace]['code'] === T_SEMICOLON) {
// Empty statement.
return;
}
$expected = $tokens[$nonSpace]['content'] . ';';
$found = $phpcsFile->getTokensAsString($nonSpace, $stackPtr - $nonSpace) . ';';
$error = 'Space found before semicolon; expected "%s" but found "%s"';
$data = array($expected, $found);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr - 1; $i > $nonSpace; $i--) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
示例8: process
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$fileName = $phpcsFile->getFilename();
$matches = array();
if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) {
// Not an actions file.
return;
}
$ownClass = $matches[2];
$tokens = $phpcsFile->getTokens();
$typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr + 2, null, false, true);
$typeName = trim($tokens[$typeName]['content'], " '");
switch (strtolower($tokens[$stackPtr + 1]['content'])) {
case 'includesystem':
$included = strtolower($typeName);
break;
case 'includeasset':
$included = strtolower($typeName) . 'assettype';
break;
case 'includewidget':
$included = strtolower($typeName) . 'widgettype';
break;
default:
return;
}
if ($included === strtolower($ownClass)) {
$error = "You do not need to include \"%s\" from within the system's own actions file";
$data = array($ownClass);
$phpcsFile->addError($error, $stackPtr, 'NotRequired', $data);
}
}
示例9: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find the content of each style definition name.
$styleNames = array();
$next = $stackPtr;
$end = $tokens[$stackPtr]['bracket_closer'];
do {
$next = $phpcsFile->findNext(array(T_STYLE, T_OPEN_CURLY_BRACKET), $next + 1, $end);
if ($next === false) {
// Class definition is empty.
break;
}
if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) {
$next = $tokens[$next]['bracket_closer'];
continue;
}
$name = $tokens[$next]['content'];
if (isset($styleNames[$name]) === true) {
$first = $styleNames[$name];
$error = 'Duplicate style definition found; first defined on line %s';
$data = array($tokens[$first]['line']);
$phpcsFile->addError($error, $next, 'Found', $data);
} else {
$styleNames[$name] = $next;
}
} while ($next !== false);
}
示例10: process
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$found = $phpcsFile->eolChar;
$found = str_replace("\n", '\\n', $found);
$found = str_replace("\r", '\\r', $found);
$phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
if ($found === $this->eolChar) {
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
// Check for single line files without an EOL. This is a very special
// case and the EOL char is set to \n when this happens.
if ($found === '\\n') {
$tokens = $phpcsFile->getTokens();
$lastToken = $phpcsFile->numTokens - 1;
if ($tokens[$lastToken]['line'] === 1 && $tokens[$lastToken]['content'] !== "\n") {
return;
}
}
$error = 'End of line character is invalid; expected "%s" but found "%s"';
$expected = $this->eolChar;
$expected = str_replace("\n", '\\n', $expected);
$expected = str_replace("\r", '\\r', $expected);
$data = array($expected, $found);
// Errors are always reported on line 1, no matter where the first PHP tag is.
$fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
if ($fix === true) {
$tokens = $phpcsFile->getTokens();
switch ($this->eolChar) {
case '\\n':
$eolChar = "\n";
break;
case '\\r':
$eolChar = "\r";
break;
case '\\r\\n':
$eolChar = "\r\n";
break;
default:
$eolChar = $this->eolChar;
break;
}
for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
if (isset($tokens[$i + 1]) === false || $tokens[$i + 1]['line'] > $tokens[$i]['line']) {
// Token is the last on a line.
if (isset($tokens[$i]['orig_content']) === true) {
$tokenContent = $tokens[$i]['orig_content'];
} else {
$tokenContent = $tokens[$i]['content'];
}
$newContent = rtrim($tokenContent, "\r\n");
$newContent .= $eolChar;
$phpcsFile->fixer->replaceToken($i, $newContent);
}
}
}
//end if
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
示例11: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip for-loop without body.
if (isset($token['scope_opener']) === false) {
return;
}
// Find incrementors for outer loop.
$outer = $this->findIncrementers($tokens, $token);
// Skip if empty.
if (count($outer) === 0) {
return;
}
// Find nested for loops.
$start = ++$token['scope_opener'];
$end = --$token['scope_closer'];
for (; $start <= $end; ++$start) {
if ($tokens[$start]['code'] !== T_FOR) {
continue;
}
$inner = $this->findIncrementers($tokens, $tokens[$start]);
$diff = array_intersect($outer, $inner);
if (count($diff) !== 0) {
$error = 'Loop incrementor (%s) jumbling with inner loop';
$data = array(join(', ', $diff));
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
}
}
}
示例12: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] === 'ob_end_flush') {
$phpcsFile->addError('Use of ob_end_flush() is not allowed; use ob_get_contents() and ob_end_clean() instead', $stackPtr, 'Found');
}
}
示例13: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$this->currentFile = $phpcsFile;
$tokens = $phpcsFile->getTokens();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
// Predicate nodes for PHP.
$find = array(T_CASE => true, T_DEFAULT => true, T_CATCH => true, T_IF => true, T_FOR => true, T_FOREACH => true, T_WHILE => true, T_DO => true, T_ELSEIF => true);
$complexity = 1;
// Iterate from start to end and count predicate nodes.
for ($i = $start + 1; $i < $end; $i++) {
if (isset($find[$tokens[$i]['code']]) === true) {
$complexity++;
}
}
if ($complexity > $this->absoluteComplexity) {
$error = 'Function\'s cyclomatic complexity (%s) exceeds allowed maximum of %s';
$data = array($complexity, $this->absoluteComplexity);
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
} else {
if ($complexity > $this->complexity) {
$warning = 'Function\'s cyclomatic complexity (%s) exceeds %s; consider refactoring the function';
$data = array($complexity, $this->complexity);
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}
示例14: processTokenWithinScope
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$tokens = $phpcsFile->getTokens();
$function = $tokens[$stackPtr + 2];
if ($function['code'] !== T_STRING) {
return;
}
$functionName = $function['content'];
$classOpener = $tokens[$currScope]['scope_condition'];
$className = $tokens[$classOpener + 2]['content'];
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if ($methodProps['is_static'] === true) {
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// There is no scope opener or closer, so the function
// must be abstract.
return;
}
$thisUsage = $stackPtr;
while (($thisUsage = $phpcsFile->findNext(array(T_VARIABLE), $thisUsage + 1, $tokens[$stackPtr]['scope_closer'], false, '$this')) !== false) {
if ($thisUsage === false) {
return;
}
$error = 'Usage of "$this" in static methods will cause runtime errors';
$phpcsFile->addError($error, $thisUsage, 'Found');
}
}
//end if
}
示例15: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
* @throws PHP_CodeSniffer_Exception If jslint.js could not be run
*/
public function process(File $phpcsFile, $stackPtr)
{
$rhinoPath = Config::getExecutablePath('jslint');
$jslintPath = Config::getExecutablePath('jslint');
if ($rhinoPath === null || $jslintPath === null) {
return;
}
$fileName = $phpcsFile->getFilename();
$cmd = "{$rhinoPath} \"{$jslintPath}\" \"{$fileName}\"";
$msg = exec($cmd, $output, $retval);
if (is_array($output) === true) {
foreach ($output as $finding) {
$matches = array();
$numMatches = preg_match('/Lint at line ([0-9]+).*:(.*)$/', $finding, $matches);
if ($numMatches === 0) {
continue;
}
$line = (int) $matches[1];
$message = 'jslint says: ' . trim($matches[2]);
$phpcsFile->addWarningOnLine($message, $line, 'ExternalTool');
}
}
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}