I'm trying to get PHP_CodeSniffer to check for camelCase in class names, however it seems to me camelCase checking is impossible (without a dictionary, including techy words).
I've raked the internet but so far the only options I've seen would be if the string had some common delimiter to explode from - i.e. underscore, space between words etc.
And even this is not useful as checking could only be accurate if the name accurately/always contained a delimiter between each word.
And the point of "checking" would be to determine if the name is not formatted correctly, and this could include not delimiting correctly.
Also, resources on PHP_CodeSniffer are either rare, or so basic and techy only the writer/developer would understand it.
Current Standard Sniff Checks
I've found this code in some of the current Sniffs (i.e. Squiz and PEAR standards):
if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false)
However, I've looked at the PHP_CodeSniffer core code and this function only does the following:
// Check the first character first.
// Check that the name only contains legal characters.
// Check that there are not two capital letters next to each other.
// The character is a number, so it cant be a capital.
These basic checks are better than nothing, although arguably useless for their supposed intended purpose, as they do not really check for camelCase at all.
The Question
How can a Sniff (or i.e. PHP script) know which "words" to check in a given string to identify if the string is 100% camelCase?
EDIT
Examples
Correct camelCase: class calculateAdminLoginCount
// Not camelCase
class calculateadminlogincount
// Partially camelCase
class calculateadminLogincount
How can the isCamelCaps()
function (or any PHP script for that matter) catch the above two examples?
How can the function or a PHP script identify "separate words" from a string, when it has no concept of "words" without feeding it that info (i.e. from a dictionary)?
Even if a script where to explode, what would it explode based on?
Take class calculateadminLogincount
How can any PHP script identify that calculate
admin
Login
count
are different words in that string to then be able to check if: 1st letter 1st word is lowercase, then all subsequent words 1st letters are uppercase?
isCamelCaps()
function
public static function isCamelCaps(
$string,
$classFormat=false,
$public=true,
$strict=true
) {
// Check the first character first.
if ($classFormat === false) {
$legalFirstChar = '';
if ($public === false) {
$legalFirstChar = '[_]';
}
if ($strict === false) {
// Can either start with a lowercase letter,
// or multiple uppercase
// in a row, representing an acronym.
$legalFirstChar .= '([A-Z]{2,}|[a-z])';
} else {
$legalFirstChar .= '[a-z]';
}
} else {
$legalFirstChar = '[A-Z]';
}
if (preg_match("/^$legalFirstChar/", $string) === 0) {
return false;
}
// Check that the name only contains legal characters.
$legalChars = 'a-zA-Z0-9';
if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) {
return false;
}
if ($strict === true) {
// Check that there are not two capital letters
// next to each other.
$length = strlen($string);
$lastCharWasCaps = $classFormat;
for ($i = 1; $i < $length; $i++) {
$ascii = ord($string{$i});
if ($ascii >= 48 && $ascii <= 57) {
// The character is a number, so it cant be a capital.
$isCaps = false;
} else {
if (strtoupper($string{$i}) === $string{$i}) {
$isCaps = true;
} else {
$isCaps = false;
}
}
if ($isCaps === true && $lastCharWasCaps === true) {
return false;
}
$lastCharWasCaps = $isCaps;
}
}//end if
return true;
}//end isCamelCaps()
EDIT 2
A little info for those wondering if this is worthwhile or not, or if I'm just "messing around" and "having fun":
It is imperative for class names to be correctly named throughout, as the file/folder structure and names and class names have to match in order for the autoloader to work solidly.
While I have checks in the Core code itself for checking and handling such issues if a script, class, etc cannot be loaded (of course), there's nothing wrong with additional scripting (PHP_CodeSniffer) to run through all files and tell me where a potential issue may lie.
Even if just for a second check, especially as it also ensures code base is tidy, correctly structured, and has continuity throughout.
isCamelCaps
tests are inadequate. What example fails those tests? – sjagr