本文整理汇总了PHP中PHP_CodeSniffer::isCamelCaps方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer::isCamelCaps方法的具体用法?PHP PHP_CodeSniffer::isCamelCaps怎么用?PHP PHP_CodeSniffer::isCamelCaps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer
的用法示例。
在下文中一共展示了PHP_CodeSniffer::isCamelCaps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processMemberVar
/**
* Processes the function tokens within the class.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = PHP_CodeSniffer_Tokens::$scopeModifiers;
$find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
$prev = $phpcsFile->findPrevious($find, $stackPtr - 1);
if ($tokens[$prev]['code'] === T_VARIABLE) {
return;
}
if ($tokens[$prev]['code'] === T_VAR) {
$error = 'The var keyword must not be used to declare a property';
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
}
$next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), $stackPtr + 1);
if ($tokens[$next]['code'] === T_VARIABLE) {
$error = 'There must not be more than one property declared per statement';
$phpcsFile->addError($error, $stackPtr, 'Multiple');
}
$modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
if ($modifier === false || $tokens[$modifier]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Visibility must be declared on property "%s"';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
}
$propertyName = ltrim($tokens[$stackPtr]['content'], '$');
if (PHP_CodeSniffer::isCamelCaps($propertyName, false, true, false) === false) {
$error = 'Property name "%s" is not in camel caps format';
$errorData = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
}
示例2: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
// Ignore magic methods.
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = strtolower(substr($methodName, 2));
if (isset($this->magicMethods[$magicPart]) === true || isset($this->methodsDoubleUnderscore[$magicPart]) === true) {
return;
}
}
$testName = ltrim($methodName, '_');
if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) {
$error = 'Method name "%s" is not in camel caps format';
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}
示例3: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$trait = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = $tokens[$trait]['content'];
if (preg_match('|^[A-Z]|', $name) === 0) {
$error = 'Each %s name must begin with a capital letter';
$errorData = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
}
if ($this->allowUnderscore === true) {
if (preg_match('|_[a-z]|', $name)) {
$error = 'Trait "%s" is not in camel caps with underscores format';
$phpcsFile->addError($error, $stackPtr, 'CamelCapsWithUnderscore', array($name));
}
$name = lcfirst(str_replace('_', '', $name));
} else {
if (strpos($name, '_') !== FALSE) {
$error = 'Underscores are not allowed in trait "%s"';
$phpcsFile->addError($error, $stackPtr, 'NoUnderscore', array($name));
}
$name = array(lcfirst($name));
}
if (PHP_CodeSniffer::isCamelCaps($name, false, true, true) === false) {
$error = 'Trait "%s" is not in camel caps format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', array($name));
}
}
示例4: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
// Is this a magic method. i.e., is prefixed with "__" ?
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = strtolower(substr($methodName, 2));
if (isset($this->magicMethods[$magicPart]) === false && isset($this->methodsDoubleUnderscore[$magicPart]) === false) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if (PHP_CodeSniffer::isCamelCaps($methodName, false, true, $this->strict) === false) {
if ($methodProps['scope_specified'] === true) {
$error = '%s method name "%s" is not in lowerCamel format';
$data = array(ucfirst($methodProps['scope']), $errorData[0]);
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
} else {
$error = 'Method name "%s" is not in lowerCamel format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
return;
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}
示例5: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
$error = 'Possible parse error: %s missing opening or closing brace';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
return;
}
// Determine the name of the class or interface. Note that we cannot
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
$name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
// Check for camel caps format.
$valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false);
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = '%s name "%s" is not in camel caps format';
$data = array($type, $name);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
}
}
示例6: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if (false !== strpos($phpcsFile->getFilename(), 'model') || false !== strpos($phpcsFile->getFilename(), 'form') || false !== strpos($phpcsFile->getFilename(), 'filter')) {
return null;
}
$tokens = $phpcsFile->getTokens();
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = trim($tokens[$className]['content']);
// "op" prefix
if (0 !== strpos($name, 'op')) {
$error = ucfirst($tokens[$stackPtr]['content']) . ' name must begin with "op" prefix';
$phpcsFile->addError($error, $stackPtr);
}
// "Interface" suffix
if ($tokens[$stackPtr]['code'] === T_INTERFACE && !preg_match('/Interface$/', $name)) {
$error = ucfirst($tokens[$stackPtr]['content']) . ' name must end with "Interface"';
$phpcsFile->addError($error, $stackPtr);
}
// stripped prefix
if (0 === strpos($name, 'op')) {
$name = substr($name, 2);
}
if (!PHP_CodeSniffer::isCamelCaps($name, true, true, false)) {
$error = ucfirst($tokens[$stackPtr]['content']) . ' name is not in camel caps format';
$phpcsFile->addError($error, $stackPtr);
}
}
示例7: validate
/**
* @param $node
* @return bool|string
*/
public function validate($node)
{
if ($node instanceof Stmt\Function_) {
if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true)) {
return [Logger::LOGLEVEL_ERROR, "Function name is not in camel caps format"];
}
return true;
}
if ($node instanceof Stmt\ClassMethod) {
if ($this->isMagicMethod($node->name)) {
return true;
}
if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true)) {
return [Logger::LOGLEVEL_ERROR, "Method name is not in camel caps format"];
}
return true;
}
if ($node instanceof Node\Expr\Variable) {
if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true) && !\PHP_CodeSniffer::isCamelCaps($node->name, true, true, true)) {
return [Logger::LOGLEVEL_WARNING, "Variable name is not in camel caps format"];
}
return true;
}
if ($node instanceof Node\Stmt\PropertyProperty) {
if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true) && !\PHP_CodeSniffer::isCamelCaps($node->name, true, true, true)) {
return [Logger::LOGLEVEL_WARNING, "Property name is not in camel caps format"];
}
return true;
}
return true;
}
示例8: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Determine the name of the class or interface. Note that we cannot
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$type = $tokens[$stackPtr]['content'];
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
$name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
// Check that each new word starts with a small latter
// and the last one with capital
$validName = true;
$nameBits = explode('_', $name);
$className = $lastBit = array_pop($nameBits);
foreach ($nameBits as $bit) {
if ($bit === '' || $bit[0] !== strtolower($bit[0])) {
$validName = false;
break;
}
}
$validName = $validName && PHP_CodeSniffer::isCamelCaps($lastBit, true, true, false);
if ($validName === false) {
// Strip underscores because they cause the suggested name
// to be incorrect.
$nameBits = explode('_', trim($name, '_'));
$lastBit = array_pop($nameBits);
if ($lastBit === '') {
$error = '%s name is not valid';
$phpcsFile->addError($error, $stackPtr, 'Invalid', array($name));
} else {
$newName = '';
foreach ($nameBits as $bit) {
if ($bit !== '') {
$newName .= strtolower($bit[0]) . substr($bit, 1) . '_';
}
}
$newName = $newName . ucfirst($lastBit);
$error = '%s name is not valid; consider %s instead';
$data = array($name);
$data[] = $newName;
$phpcsFile->addError($error, $stackPtr, 'Invalid', $data);
}
}
if ($type === 'interface') {
if (substr($className, 0, 1) !== 'I') {
$error = 'Interface name %s must start with "I"';
$data = array($name);
$phpcsFile->addError($error, $stackPtr, 'BadInterfaceName', $data);
} else {
if (!PHP_CodeSniffer::isCamelCaps(substr($className, 1), true, true, false)) {
$phpcsFile->addError('Interface name %s is not in camel caps format', $stackPtr, 'NotCamelCaps', $className);
}
}
}
}
示例9: processTokenWithinScope
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if (!PHP_CodeSniffer::isCamelCaps($methodName, false, true, false)) {
$error = $className . '::' . $methodName . '() name is not in camel caps format';
$phpcsFile->addError($error, $stackPtr);
}
}
示例10: processMemberVar
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$name = $tokens[$stackPtr]['content'];
if (!PHP_CodeSniffer::isCamelCaps(substr($name, 1), false, true, false)) {
$error = $name . ' name is not in camel caps format';
$phpcsFile->addError($error, $stackPtr);
}
}
示例11: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->_magicMethods) === false) {
$error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$isPublic = $methodProps['scope'] === 'public' ? true : false;
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// If it's a private method, it must have an underscore on the front.
if ($isPublic === false && $methodName[0] !== '_') {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// If it's not a private method, it must not have an underscore on the front.
if ($isPublic === true && $scopeSpecified === true && $methodName[0] === '_') {
$error = "Public method name \"{$className}::{$methodName}\" must not be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// If the scope was specified on the method, then the method must be
// camel caps and an underscore should be checked for. If it wasn't
// specified, treat it like a public method and remove the underscore
// prefix if there is one because we cant determine if it is private or
// public.
$testMethodName = $methodName;
if ($scopeSpecified === false && $methodName[0] === '_') {
$testMethodName = substr($methodName, 1);
}
if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
if ($scopeSpecified === true) {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format";
} else {
$error = "Method name \"{$className}::{$methodName}\" is not in camel caps format";
}
$phpcsFile->addError($error, $stackPtr);
return;
}
}
示例12: validate
/**
* @param PHP_CodeSniffer_File $phpcsFile
* @param integer $stackPtr
* @param string $name
*/
protected function validate(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name)
{
$reserved = ["_SERVER", "_GET", "_POST", "_REQUEST", "_SESSION", "_ENV", "_COOKIE", "_FILES", "GLOBALS"];
if (in_array($name, $reserved)) {
return;
}
if (PHP_CodeSniffer::isCamelCaps($name, false, true, false)) {
return;
}
$phpcsFile->addError("Variable \"{$name}\" is an invalid name", $stackPtr);
}
示例13: processMemberVar
/**
* {@inheritdoc}
*/
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
$error = 'Variable "%s" is not in valid camel caps format';
$data = array($varName);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
} elseif (preg_match('|\\d|', $varName)) {
$warning = 'Variable "%s" contains numbers but this is discouraged';
$data = array($varName);
$phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
}
}
示例14: processTokenWithinScope
/**
* Override parent function to allow custom code validation
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->magicMethods) === false) {
$error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$isPublic = $methodProps['scope'] === 'private' ? false : true;
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// If it's a private method, it must have an underscore on the front.
if ($isPublic === false && $methodName[0] !== '_') {
$error = "Private method name \"{$className}::{$methodName}\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// We want to allow private and protected methods to start with an underscore
$testMethodName = $methodName;
if (($scopeSpecified === false || $scope == 'protected') && $methodName[0] === '_') {
$testMethodName = substr($methodName, 1);
}
if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
if ($scopeSpecified === true) {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format";
} else {
$error = "Method name \"{$className}::{$methodName}\" is not in camel caps format";
}
$phpcsFile->addError($error, $stackPtr);
return;
}
}
示例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
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName === null) {
return;
}
$errorData = array($functionName);
if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) {
if (!preg_match('~^get|set|scenario|button|__~', $functionName)) {
$error = 'Function name "%s" is not in camel caps format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
}
}