Skip to content

Commit

Permalink
Merge branch 'feature/1880-validvariablename-dont-hide-one-message-be…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Feb 19, 2018
2 parents 10fadb1 + ccacdd8 commit 8d132e5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
// 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
if (isset($this->magicMethods[$magicPart]) === true
|| isset($this->methodsDoubleUnderscore[$magicPart]) === true
) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
return;
}

return;
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}

// PHP4 constructors are allowed to break our rules.
Expand Down Expand Up @@ -178,12 +178,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
// Is this a magic function. i.e., it is prefixed with "__".
if (preg_match('|^__[^_]|', $functionName) !== 0) {
$magicPart = strtolower(substr($functionName, 2));
if (isset($this->magicFunctions[$magicPart]) === false) {
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
if (isset($this->magicFunctions[$magicPart]) === true) {
return;
}

return;
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
}

// Ignore first underscore in functions prefixed with "_".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getErrorList()
31 => 1,
50 => 1,
52 => 1,
53 => 1,
53 => 2,
57 => 1,
58 => 1,
59 => 1,
Expand All @@ -52,16 +52,16 @@ public function getErrorList()
65 => 1,
66 => 1,
67 => 1,
68 => 1,
68 => 2,
69 => 1,
71 => 1,
72 => 1,
73 => 1,
73 => 2,
74 => 1,
118 => 1,
144 => 1,
146 => 1,
147 => 1,
147 => 2,
];

return $errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
// 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) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
if (isset($this->magicMethods[$magicPart]) === true) {
return;
}

return;
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}

// PHP4 constructors are allowed to break our rules.
Expand Down Expand Up @@ -116,7 +116,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
$error = 'Private method name "%s" must be prefixed with an underscore';
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no');
return;
} else {
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes');
}
Expand All @@ -130,20 +129,11 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
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);
}
$testMethodName = ltrim($methodName, '_');

if (Common::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
if (Common::isCamelCaps($testMethodName, false, true, false) === false) {
if ($scopeSpecified === true) {
$error = '%s method name "%s" is not in camel caps format';
$data = [
Expand All @@ -155,8 +145,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
$error = 'Method name "%s" is not in camel caps format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}

return;
}

}//end processTokenWithinScope()
Expand Down Expand Up @@ -189,12 +177,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
// Is this a magic function. i.e., it is prefixed with "__".
if (preg_match('|^__[^_]|', $functionName) !== 0) {
$magicPart = strtolower(substr($functionName, 2));
if (isset($this->magicFunctions[$magicPart]) === false) {
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
if (isset($this->magicFunctions[$magicPart]) === true) {
return;
}

return;
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
}

// Function names can be in two parts; the package name and
Expand All @@ -216,13 +204,11 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
if ($functionName{0} === '_') {
$error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
return;
}

if ($functionName{0} !== strtoupper($functionName{0})) {
$error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
$phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ public function getErrorList()
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
17 => 2,
18 => 2,
19 => 2,
20 => 2,
24 => 1,
25 => 1,
26 => 1,
27 => 1,
28 => 1,
29 => 1,
30 => 1,
31 => 1,
32 => 1,
33 => 1,
30 => 2,
31 => 2,
32 => 2,
33 => 2,
35 => 1,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
40 => 1,
37 => 2,
38 => 2,
39 => 2,
40 => 2,
43 => 1,
44 => 1,
45 => 1,
Expand All @@ -70,26 +70,26 @@ public function getErrorList()
70 => 1,
71 => 1,
72 => 1,
73 => 1,
74 => 1,
75 => 1,
76 => 1,
73 => 2,
74 => 2,
75 => 2,
76 => 2,
80 => 1,
81 => 1,
82 => 1,
83 => 1,
84 => 1,
85 => 1,
86 => 1,
87 => 1,
88 => 1,
89 => 1,
86 => 2,
87 => 2,
88 => 2,
89 => 2,
91 => 1,
92 => 1,
93 => 1,
94 => 1,
95 => 1,
96 => 1,
93 => 2,
94 => 2,
95 => 2,
96 => 2,
99 => 1,
100 => 1,
101 => 1,
Expand All @@ -108,13 +108,13 @@ public function getErrorList()
124 => 1,
125 => 1,
126 => 1,
127 => 1,
128 => 1,
129 => 1,
130 => 1,
127 => 2,
128 => 2,
129 => 2,
130 => 2,
149 => 1,
151 => 1,
152 => 1,
152 => 2,
155 => 1,
156 => 1,
157 => 1,
Expand All @@ -126,18 +126,18 @@ public function getErrorList()
163 => 1,
164 => 1,
165 => 1,
166 => 1,
166 => 3,
167 => 1,
169 => 1,
170 => 1,
171 => 1,
173 => 1,
171 => 3,
173 => 2,
174 => 1,
175 => 1,
207 => 1,
227 => 1,
229 => 1,
230 => 1,
230 => 2,
];

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
if (preg_match('|^__[^_]|', $functionName) !== 0) {
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData);
return;

$functionName = ltrim($functionName, '_');
}

if (Common::isCamelCaps($functionName, false, true, false) === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,18 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
return;
}
} else {
if (substr($varName, 0, 1) !== '_') {
$error = 'Private member variable "%s" must contain a leading underscore';
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
return;
}
}

if (Common::isCamelCaps($varName, false, $public, false) === false) {
// Remove a potential underscore prefix for testing CamelCaps.
$varName = ltrim($varName, '_');

if (Common::isCamelCaps($varName, false, true, false) === false) {
$error = 'Member variable "%s" is not in valid camel caps format';
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getErrorList()
11 => 1,
12 => 1,
13 => 1,
14 => 1,
14 => 2,
];

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getErrorList()
67 => 1,
81 => 1,
106 => 1,
107 => 1,
107 => 2,
108 => 1,
117 => 1,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
$error = 'Public member variable "%s" must not contain a leading underscore';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
return;
}
} else {
if (substr($varName, 0, 1) !== '_') {
Expand All @@ -152,11 +151,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
$varName,
];
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
return;
}
}

if (Common::isCamelCaps($varName, false, $public, false) === false) {
// Remove a potential underscore prefix for testing CamelCaps.
$varName = ltrim($varName, '_');

if (Common::isCamelCaps($varName, false, true, false) === false) {
$error = 'Member variable "%s" is not in valid camel caps format';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
Expand Down

0 comments on commit 8d132e5

Please sign in to comment.