-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypoCheckUtils.php
189 lines (178 loc) · 6.98 KB
/
TypoCheckUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php declare(strict_types=1);
namespace PhanTypoCheck;
use InvalidArgumentException;
use RuntimeException;
use function array_map;
use function array_pop;
use function array_values;
use function count;
use function dirname;
use function explode;
use function file_get_contents;
use function implode;
use function is_array;
use function preg_match;
use function preg_match_all;
use function stripos;
use function strtolower;
use function substr;
use function substr_count;
use function token_get_all;
use function trim;
use const PREG_OFFSET_CAPTURE;
require_once __DIR__ . '/TypoDetails.php';
require_once __DIR__ . '/StringUtil.php';
/**
* This contains the parts of PhanTypoCheck that aren't related to Phan or php-ast.
*
* ------------------------
*
* PhanTypoCheck is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PhanTypoCheck is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PhanTypoCheck. If not, see <https://www.gnu.org/licenses/>.
*/
class TypoCheckUtils
{
/** @var ?array<string,array<int,string>> */
private static $dictionary = null;
public static function getDictionary()
{
return self::$dictionary ?? (self::$dictionary = self::loadDictionary());
}
private static function loadDictionary() : array
{
$file = dirname(__DIR__) . '/data/dictionary.txt';
$contents = file_get_contents($file);
if (!$contents) {
throw new RuntimeException("failed to load $file");
}
$result = [];
foreach (explode("\n", $contents) as $line) {
$line = trim($line);
if (stripos($line, '->') === false) {
continue;
}
list($typo, $corrections_string) = explode('->', $line, 2);
$corrections = explode(',', $corrections_string);
$result[$typo] = $corrections;
}
return $result;
}
/**
* @return array<int,TypoDetails>
* Maps line number to details about that typo
*/
public static function getTyposForText(string $contents) : array
{
$dictionary = self::getDictionary();
$results = [];
$analyze_text = static function (string $text, array $token) use ($dictionary, &$results) {
preg_match_all('/[a-z0-9]{3,}(?:\'[a-z]+)?/i', $text, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $match) {
list($word, $offset) = $match;
$suggestions = $dictionary[strtolower($word)] ?? null;
if ($suggestions === null) {
continue;
}
// Edge case in php 7.0: warns if length is 0
$lineno = (int)($token[2]) + ($offset > 0 ? substr_count($text, "\n", 0, $offset) : 0);
$results[] = new TypoDetails($word, $token, $lineno, $suggestions);
}
};
$analyze_identifier = static function (string $text, array $token) use ($dictionary, &$results) {
// Try to extract everything from identifiers that are CamelCase, camelCase, or camelACRONYMCase.
preg_match_all('/[a-z]+|[A-Z](?:[a-z]+|[A-Z]+(?![a-z]))/', $text, $matches);
foreach ($matches[0] as $word) {
$suggestions = $dictionary[strtolower($word)] ?? null;
if ($suggestions === null) {
continue;
}
$did_skip_word_with_apostrophe = false;
foreach ($suggestions as $i => $suggestion) {
if (!preg_match('/[^a-zA-Z0-9_\x7f-\xff]/', $suggestion)) {
// This replacement definitely isn't a valid php token (e.g. has `'` or `-`)
continue;
}
if (count($suggestions) < 2 || $i !== count($suggestions) - 1) {
$did_skip_word_with_apostrophe = true;
unset($suggestions[$i]);
}
}
if ($did_skip_word_with_apostrophe) {
if (count($suggestions) <= 1) {
// the last value is always the empty string or a reason to consider not fixing it
continue;
}
$suggestions = array_values($suggestions);
}
$lineno = (int)($token[2]);
$results[] = new TypoDetails($word, $token, $lineno, $suggestions);
}
};
foreach (@token_get_all($contents) as $token) {
if (!is_array($token)) {
continue;
}
switch ($token[0]) {
case \T_ENCAPSED_AND_WHITESPACE:
try {
$text = StringUtil::parseEscapeSequences($token[1], '"');
} catch (InvalidArgumentException $_) {
break;
}
$analyze_text($text, $token);
// decode
break;
case \T_CONSTANT_ENCAPSED_STRING:
$text = StringUtil::parse($token[1]);
$analyze_text($text, $token);
break;
case \T_VARIABLE:
$analyze_identifier((string)substr($token[1], 1), $token);
break;
case \T_STRING:
$analyze_identifier($token[1], $token);
break;
case \T_COMMENT:
case \T_DOC_COMMENT:
case \T_INLINE_HTML:
$analyze_text($token[1], $token);
break;
}
}
return $results;
}
public static function makeSuggestionText(array $suggestions, string $original_word) : string
{
$suggestions = array_map('trim', $suggestions);
if (count($suggestions) > 1) {
// empty string for no reason
$reason = array_pop($suggestions);
} else {
$reason = null;
}
if (preg_match('/[a-z]/', $original_word, $matches, PREG_OFFSET_CAPTURE)) {
if ($matches[0][1] > 0) {
// The first character of the original word is uppercase
$suggestions = array_map('ucfirst', $suggestions);
}
} else {
// The word is uppercase
$suggestions = array_map('strtoupper', $suggestions);
}
$suggestion = 'Did you mean ' . implode(' or ', array_map('json_encode', $suggestions)) . '?';
if ($reason) {
$suggestion .= " : not always fixable: $reason";
}
return $suggestion;
}
}