/** * Processes each required or optional tag. * * @param int $commentstart The position in the stack where the comment started. * @param int $commentend The position in the stack where the comment ended. * * @return void */ protected function processtags($commentstart, $commentend) { $docblock = get_class($this) === 'moodle_sniffs_commenting_filecommentsniff' ? 'file' : 'class'; $foundtags = $this->commentparser->gettagOrders(); $orderindex = 0; $indentation = array(); $longesttag = 0; $errorpos = 0; foreach ($this->tags as $tag => $info) { // Required tag missing. if ($info['required'] === true && in_array($tag, $foundtags) === false) { $error = "Missing @{$tag} tag in {$docblock} comment"; $this->currentfile->adderror($error, $commentend); continue; } // Get the line number for current tag. $tagname = ucfirst($tag); if ($info['allow_multiple'] === true) { $tagname .= 's'; } $getmethod = 'get' . $tagname; $tagelement = $this->commentparser->{$getmethod}(); if (is_null($tagelement) === true || empty($tagelement) === true) { continue; } $errorpos = $commentstart; if (is_array($tagelement) === false) { $errorpos = $commentstart + $tagelement->getline(); } // Get the tag order. $foundindexes = array_keys($foundtags, $tag); if (count($foundindexes) > 1) { // Multiple occurance not allowed. if ($info['allow_multiple'] === false) { $error = "Only 1 @{$tag} tag is allowed in a {$docblock} comment"; $this->currentfile->adderror($error, $errorpos); } else { // Make sure same tags are grouped together. $i = 0; $count = $foundindexes[0]; foreach ($foundindexes as $index) { if ($index !== $count) { $errorposindex = $errorpos + $tagelement[$i]->getline(); $error = "@{$tag} tags must be grouped together"; $this->currentfile->adderror($error, $errorposindex); } $i++; $count++; } } } // Check tag order. if ($foundindexes[0] > $orderindex) { $orderindex = $foundindexes[0]; } else { if (is_array($tagelement) === true && empty($tagelement) === false) { $errorpos += $tagelement[0]->getline(); } $ordertext = $info['order_text']; $error = "The @{$tag} tag is in the wrong order; the tag {$ordertext}"; $this->currentfile->adderror($error, $errorpos); } // Store the indentation for checking. $len = strlen($tag); if ($len > $longesttag) { $longesttag = $len; } if (is_array($tagelement) === true) { foreach ($tagelement as $key => $element) { $indentation[] = array('tag' => $tag, 'space' => $this->getIndentation($tag, $element), 'line' => $element->getline()); } } else { $indentation[] = array('tag' => $tag, 'space' => $this->getIndentation($tag, $tagelement)); } $method = 'process' . $tagname; if (method_exists($this, $method) === true) { // Process each tag if a method is defined. call_user_func(array($this, $method), $errorpos); } else { if (is_array($tagelement) === true) { foreach ($tagelement as $key => $element) { $element->process($this->currentfile, $commentstart, $docblock); } } else { $tagelement->process($this->currentfile, $commentstart, $docblock); } } } foreach ($indentation as $indentinfo) { if ($indentinfo['space'] !== 0 && $indentinfo['space'] !== $longesttag + 1) { $expected = $longesttag - strlen($indentinfo['tag']) + 1; $space = $indentinfo['space'] - strlen($indentinfo['tag']); $error = "@{$indentinfo['tag']} tag comment indented incorrectly. "; $error .= "Expected {$expected} spaces but found {$space}."; $gettagmethod = 'get' . ucfirst($indentinfo['tag']); if ($this->tags[$indentinfo['tag']]['allow_multiple'] === true) { $line = $indentinfo['line']; } else { $tagelem = $this->commentparser->{$gettagmethod}(); $line = $tagelem->getline(); } $this->currentfile->addwarning($error, $commentstart + $line); } } }