Skip to content

Commit 9803674

Browse files
author
epriestley
committedNov 8, 2016
Extract variable type information from pht() calls
Summary: Ref T5267. When extrating data from `pht()` calls, also extract the argument types and export them into the map so they can be used by consumers. We recognize plurals (`phutil_count()`, `new PhutilNumber`) and genders (`phutil_person()`). We'll need to annotate the codebase for those, since they're currently runtime-only. Test Plan: Rebuilt extraction maps, got data like this (note "number" type annotation). ``` "Scaling pool \"%s\" up to %s daemon(s).": { "uses": [ { "file": "/daemon/PhutilDaemonOverseer.php", "line": 378 } ], "types": [ null, "number" ] }, ``` Reviewers: chad Reviewed By: chad Maniphest Tasks: T5267 Differential Revision: https://secure.phabricator.com/D16823
1 parent 3f5109b commit 9803674

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
 

‎src/applications/people/storage/PhabricatorUser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public function overrideTimezoneIdentifier($identifier) {
572572
return $this;
573573
}
574574

575-
public function getSex() {
575+
public function getGender() {
576576
return $this->getUserSetting(PhabricatorPronounSetting::SETTINGKEY);
577577
}
578578

‎src/infrastructure/internationalization/management/PhabricatorInternationalizationManagementExtractWorkflow.php

+53-1
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,49 @@ private function extractFiles($root_path, array $files) {
175175
try {
176176
$string_value = $string_node->evalStatic();
177177

178+
$args = $params->getChildren();
179+
$args = array_slice($args, 1);
180+
181+
$types = array();
182+
foreach ($args as $child) {
183+
$type = null;
184+
185+
switch ($child->getTypeName()) {
186+
case 'n_FUNCTION_CALL':
187+
$call = $child->getChildByIndex(0);
188+
if ($call->getTypeName() == 'n_SYMBOL_NAME') {
189+
switch ($call->getConcreteString()) {
190+
case 'phutil_count':
191+
$type = 'number';
192+
break;
193+
case 'phutil_person':
194+
$type = 'person';
195+
break;
196+
}
197+
}
198+
break;
199+
case 'n_NEW':
200+
$class = $child->getChildByIndex(0);
201+
if ($class->getTypeName() == 'n_CLASS_NAME') {
202+
switch ($class->getConcreteString()) {
203+
case 'PhutilNumber':
204+
$type = 'number';
205+
break;
206+
}
207+
}
208+
break;
209+
default:
210+
break;
211+
}
212+
213+
$types[] = $type;
214+
}
215+
178216
$results[$hash][] = array(
179217
'string' => $string_value,
180218
'file' => Filesystem::readablePath($full_path, $root_path),
181219
'line' => $string_line,
220+
'types' => $types,
182221
);
183222
} catch (Exception $ex) {
184223
$messages[] = pht(
@@ -207,10 +246,23 @@ private function writeStrings($root, array $strings) {
207246
$map = array();
208247
foreach ($strings as $hash => $string_list) {
209248
foreach ($string_list as $string_info) {
210-
$map[$string_info['string']]['uses'][] = array(
249+
$string = $string_info['string'];
250+
251+
$map[$string]['uses'][] = array(
211252
'file' => $string_info['file'],
212253
'line' => $string_info['line'],
213254
);
255+
256+
if (!isset($map[$string]['types'])) {
257+
$map[$string]['types'] = $string_info['types'];
258+
} else if ($map[$string]['types'] !== $string_info['types']) {
259+
echo tsprintf(
260+
"**<bg:yellow> %s </bg>** %s\n",
261+
pht('WARNING'),
262+
pht(
263+
'Inferred types for string "%s" vary across callsites.',
264+
$string_info['string']));
265+
}
214266
}
215267
}
216268

0 commit comments

Comments
 (0)
Failed to load comments.