diff --git a/peck.json b/peck.json index eaa17abf..41ecc4e9 100644 --- a/peck.json +++ b/peck.json @@ -2,7 +2,9 @@ "preset": "base", "ignore": { "words": [ - "php" + "php", + "strtolower", + "fg" ], "paths": [ "tests" diff --git a/src/Checkers/SourceCodeChecker.php b/src/Checkers/SourceCodeChecker.php index 7dc7ac39..dfc6eb81 100644 --- a/src/Checkers/SourceCodeChecker.php +++ b/src/Checkers/SourceCodeChecker.php @@ -19,6 +19,7 @@ use ReflectionProperty; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; +use Throwable; /** * @internal @@ -92,6 +93,7 @@ private function getIssuesFromSourceFile(SplFileInfo $file): array ...$this->getMethodNames($reflection), ...$this->getPropertyNames($reflection), ...$this->getConstantNames($reflection), + ...$this->getStringValues($file), ]; if ($docComment = $reflection->getDocComment()) { @@ -125,6 +127,36 @@ private function getIssuesFromSourceFile(SplFileInfo $file): array return $issues; } + /** + * Get the string values contained in the given file. + * + * @return array + */ + private function getStringValues(SplFileInfo $file): array + { + try { + $tokens = token_get_all($file->getContents()); + } catch (Throwable) { + return []; + } + + foreach ($tokens as $token) { + if (is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) { + // Remove the surrounding quotes from the string + $string = substr($token[1], 1, -1); + + // Handle escaped quotes depending on string type + if ($token[1][0] === '"') { + $string = stripcslashes($string); + } + + $stringsToCheck[] = $string; + } + } + + return $stringsToCheck ?? []; + } + /** * Get the method names contained in the given reflection. * diff --git a/stubs/presets/base.stub b/stubs/presets/base.stub index 0a04423c..61ba69e9 100644 --- a/stubs/presets/base.stub +++ b/stubs/presets/base.stub @@ -119,6 +119,7 @@ sqs src ssl sso +str stringable symfony throwable @@ -139,6 +140,7 @@ unformatted unsuspend upsert uri +utf uuid validator verifier