Skip to content

Commit 5b5e75d

Browse files
committed
WIP refactored package parsing into getPackage() method
1 parent c395ff8 commit 5b5e75d

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,68 @@ private function getEndLine(array $tokens, $idx)
310310
return $this->tline($tokens[$this->getEndTokenId($tokens, $idx)]);
311311
}
312312

313+
/**
314+
* @return array
315+
*/
316+
private function getPackage(array $tokens, $idx)
317+
{
318+
$token = $tokens[$idx];
319+
$className = $this->tname($token);
320+
$docComment = $this->getDocblock($tokens, $idx);
321+
322+
$result = array(
323+
'namespace' => '',
324+
'fullPackage' => '',
325+
'category' => '',
326+
'package' => '',
327+
'subpackage' => ''
328+
);
329+
330+
for ($i = $idx; $i; --$i) {
331+
$tconst = $this->tconst($tokens[$i]);
332+
if ($tconst === T_NAMESPACE) {
333+
$result['namespace'] = $this->tname($tokens[$i]);
334+
break;
335+
}
336+
}
337+
338+
if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) {
339+
$result['category'] = $matches[1];
340+
}
341+
342+
if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) {
343+
$result['package'] = $matches[1];
344+
$result['fullPackage'] = $matches[1];
345+
}
346+
347+
if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) {
348+
$result['subpackage'] = $matches[1];
349+
$result['fullPackage'] .= '.' . $matches[1];
350+
}
351+
352+
if (empty($result['fullPackage'])) {
353+
$result['fullPackage'] = $this->arrayToName(
354+
explode('_', str_replace('\\', '_', $className)),
355+
'.'
356+
);
357+
}
358+
359+
return $result;
360+
}
361+
362+
private function arrayToName(array $parts, $join = '\\')
363+
{
364+
$result = '';
365+
366+
if (count($parts) > 1) {
367+
array_pop($parts);
368+
369+
$result = join($join, $parts);
370+
}
371+
372+
return $result;
373+
}
374+
313375
public function tokenize() {
314376
$sourceCode = file_get_contents($this->filename);
315377
$tokens = token_get_all($sourceCode);
@@ -363,7 +425,7 @@ public function tokenize() {
363425
'docblock' => $this->getDocblock($tokens, $i),
364426
'startLine' => $this->tline($token),
365427
'endLine' => $endLine,
366-
'package' => $token->getPackage(),
428+
'package' => $this->getPackage($tokens, $i),
367429
'file' => $this->filename
368430
);
369431

0 commit comments

Comments
 (0)