Hoa central
PhpdocVar.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use SplFileInfo;
44 
61 class PhpdocVar extends AbstractFixer
62 {
63  public function fix(SplFileInfo $file, $content)
64  {
65  $tokens = Tokens::fromCode($content);
66 
67  foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) {
68  $docBlock = new DocBlock($token->getContent());
69  $annotations = $docBlock->getAnnotationsOfType('var');
70 
71  if (empty($annotations)) {
72  continue;
73  }
74 
75  foreach ($annotations as $annotation) {
76  $line = $docBlock->getLine($annotation->getStart());
77  $lineContent = $line->getContent();
78 
79  if (0 !== preg_match('/^(?<before>.*?@var )(?<one>[^\s]+) (?<two>\w+)/', $lineContent, $matches)) {
80  if ('object' === $matches['two']) {
81  $line->setContent($matches['before'] . $matches['one'] . "\n");
82  } else {
83  $line->setContent($matches['before'] . $matches['two'] . "\n");
84  }
85  }
86  }
87 
88  $token->setContent($docBlock->getContent());
89  }
90 
91  return $tokens->generateCode();
92  }
93 
94  public function getDescription()
95  {
96  return '`@var` must contain one element.';
97  }
98 
99  public function getName()
100  {
101  return 'phpdoc_var';
102  }
103 
104  public function getLevel()
105  {
106  return FixerInterface::CONTRIB_LEVEL;
107  }
108 }
$content
Definition: Hoa.php:119