Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion peck.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"preset": "base",
"ignore": {
"words": [
"php"
"php",
"strtolower",
"fg"
],
"paths": [
"tests"
Expand Down
32 changes: 32 additions & 0 deletions src/Checkers/SourceCodeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ReflectionProperty;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;

/**
* @internal
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -125,6 +127,36 @@ private function getIssuesFromSourceFile(SplFileInfo $file): array
return $issues;
}

/**
* Get the string values contained in the given file.
*
* @return array<int, string>
*/
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.
*
Expand Down
2 changes: 2 additions & 0 deletions stubs/presets/base.stub
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ sqs
src
ssl
sso
str
stringable
symfony
throwable
Expand All @@ -139,6 +140,7 @@ unformatted
unsuspend
upsert
uri
utf
uuid
validator
verifier
Expand Down
Loading