Skip to content

Commit 4c92103

Browse files
committed
Generic/LowerCaseType: refactor [2] - remove duplicate code
Extract duplicate code out to a separate method and call the method instead.
1 parent 7e0f822 commit 4c92103

File tree

1 file changed

+66
-68
lines changed

1 file changed

+66
-68
lines changed

src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,20 @@ public function process(File $phpcsFile, $stackPtr)
7070

7171
if (isset(Tokens::$castTokens[$tokens[$stackPtr]['code']]) === true) {
7272
// A cast token.
73-
if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) {
74-
if ($tokens[$stackPtr]['content'] === strtoupper($tokens[$stackPtr]['content'])) {
75-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper');
76-
} else {
77-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed');
78-
}
79-
80-
$error = 'PHP type casts must be lowercase; expected "%s" but found "%s"';
81-
$data = [
82-
strtolower($tokens[$stackPtr]['content']),
83-
$tokens[$stackPtr]['content'],
84-
];
85-
86-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'TypeCastFound', $data);
87-
if ($fix === true) {
88-
$phpcsFile->fixer->replaceToken($stackPtr, strtolower($tokens[$stackPtr]['content']));
89-
}
90-
} else {
91-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower');
92-
}//end if
73+
$this->processType(
74+
$phpcsFile,
75+
$stackPtr,
76+
$tokens[$stackPtr]['content'],
77+
'PHP type casts must be lowercase; expected "%s" but found "%s"',
78+
'TypeCastFound'
79+
);
9380

9481
return;
95-
}//end if
82+
}
83+
84+
/*
85+
* Check function return type.
86+
*/
9687

9788
$props = $phpcsFile->getMethodProperties($stackPtr);
9889

@@ -103,29 +94,15 @@ public function process(File $phpcsFile, $stackPtr)
10394
if ($returnType !== ''
10495
&& isset($this->phpTypes[$returnTypeLower]) === true
10596
) {
106-
// A function return type.
107-
if ($returnTypeLower !== $returnType) {
108-
if ($returnType === strtoupper($returnType)) {
109-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper');
110-
} else {
111-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed');
112-
}
113-
114-
$error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s"';
115-
$token = $props['return_type_token'];
116-
$data = [
117-
$returnTypeLower,
118-
$returnType,
119-
];
120-
121-
$fix = $phpcsFile->addFixableError($error, $token, 'ReturnTypeFound', $data);
122-
if ($fix === true) {
123-
$phpcsFile->fixer->replaceToken($token, $returnTypeLower);
124-
}
125-
} else {
126-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower');
127-
}//end if
128-
}//end if
97+
$error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s"';
98+
$errorCode = 'ReturnTypeFound';
99+
100+
$this->processType($phpcsFile, $props['return_type_token'], $returnType, $error, $errorCode);
101+
}
102+
103+
/*
104+
* Check function parameter types.
105+
*/
129106

130107
$params = $phpcsFile->getMethodParameters($stackPtr);
131108
if (empty($params) === true) {
@@ -140,32 +117,53 @@ public function process(File $phpcsFile, $stackPtr)
140117
if ($typeHint !== ''
141118
&& isset($this->phpTypes[$typeHintLower]) === true
142119
) {
143-
// A function return type.
144-
if ($typeHintLower !== $typeHint) {
145-
if ($typeHint === strtoupper($typeHint)) {
146-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper');
147-
} else {
148-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed');
149-
}
150-
151-
$error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s"';
152-
$token = $param['type_hint_token'];
153-
$data = [
154-
$typeHintLower,
155-
$typeHint,
156-
];
157-
158-
$fix = $phpcsFile->addFixableError($error, $token, 'ParamTypeFound', $data);
159-
if ($fix === true) {
160-
$phpcsFile->fixer->replaceToken($token, $typeHintLower);
161-
}
162-
} else {
163-
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower');
164-
}//end if
165-
}//end if
166-
}//end foreach
120+
$error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s"';
121+
$errorCode = 'ParamTypeFound';
122+
123+
$this->processType($phpcsFile, $param['type_hint_token'], $typeHint, $error, $errorCode);
124+
}
125+
}
167126

168127
}//end process()
169128

170129

130+
/**
131+
* Processes a type cast or a singular type declaration.
132+
*
133+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
134+
* @param int $stackPtr The position of the type token.
135+
* @param string $type The type found.
136+
* @param string $error Error message template.
137+
* @param string $errorCode The error code.
138+
*
139+
* @return void
140+
*/
141+
protected function processType(File $phpcsFile, $stackPtr, $type, $error, $errorCode)
142+
{
143+
$typeLower = strtolower($type);
144+
145+
if ($typeLower === $type) {
146+
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower');
147+
return;
148+
}
149+
150+
if ($type === strtoupper($type)) {
151+
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper');
152+
} else {
153+
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed');
154+
}
155+
156+
$data = [
157+
$typeLower,
158+
$type,
159+
];
160+
161+
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data);
162+
if ($fix === true) {
163+
$phpcsFile->fixer->replaceToken($stackPtr, $typeLower);
164+
}
165+
166+
}//end processType()
167+
168+
171169
}//end class

0 commit comments

Comments
 (0)