<commit_before><?php
namespace Mill;

use gossi\docblock\Docblock;
use gossi\docblock\tags\UnknownTag;
use Mill\Exceptions\Resource\UnsupportedDecoratorException;
use Mill\Parser\Annotation;
use Mill\Parser\MSON;
use Mill\Parser\Version;
use ReflectionClass;

/**
 * Class for tokenizing a docblock on a given class or method.
 *
 */
class Parser
{
    const REGEX_DECORATOR = '/^(?P<decorator>(:\w+)+)?/u';

    /**
     * The current class that we're going to be parsing.
     *
     * @var string
     */
    protected $class;

    /**
     * The current class method that we're parsing. Used to give better error messaging.
     *
     * @var null|string
     */
    protected $method;

    /**
     * @param string $class
     */
    public function __construct(string $class)
    {
        $this->class = $class;
    }

    /**
     * Get an array of HTTP (GET, POST, PUT, PATCH, DELETE) methods that are implemented on the current class.
     *
     * @return array
     */
    public function getHttpMethods()
    {
        $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];

        $reflection = new ReflectionClass($this->class);

        $valid_methods = [];
        foreach ($methods as $method) {
            if ($reflection->hasMethod($method)) {
                $valid_methods[] = $method;
            }
        }

        return $valid_methods;
    }

    /**
     * Locate, and parse, the annotations for a class or method.
     *
     * @param null|string $method_name
     * @return array An array containing all the found annotations.
     */
    public function getAnnotations(string $method_name = null): array
    {
        if (!empty($method_name)) {
            $this->method = $method_name;
        }

        $reader = Container::getAnnotationReader();
        $comments = $reader($this->class, $method_name);

        if (empty($comments)) {
            return [];
        }

        return $this->parseDocblock($comments);
    }

    /**
     * Parse a docblock comment into its parts.
     *
     * @link https://github.com/facebook/libphutil/blob/master/src/parser/docblock/PhutilDocblockParser.php
     * @param string $docblock
     * @param boolean $parse_description If we want to parse out an unstructured `description` annotation.
     * @return array Array of parsed annotations.
     */
    protected function parseDocblock(string $docblock, bool $parse_description = true): array
    {
        $original_docblock = $docblock;
        $annotations = [];
        $matches = null;

        $parser = self::getAnnotationsFromDocblock($docblock);
        $tags = $parser->getTags();
        if (!empty($tags)) {
            $annotation_tags = [];

            /** @var UnknownTag $tag */
            foreach ($tags as $tag) {
                // If this isn't a Mill annotation, then ignore it.
                $annotation = $tag->getTagName();
                if (substr($annotation, 0, 4) !== 'api-') {
                    continue;
                }

                $annotation_tags[] = $tag;
            }

            $annotations = $this->parseAnnotations($annotation_tags, $original_docblock);
        }

        // Only parse out a `description` annotation if we need to (like in the instance of not parsing a
        // representation).
        if (!$parse_description) {
            return $annotations;
        }

        // Reconstruct the description as the developer wrote it.
        $description = implode("\n\n", array_filter([
            $parser->getShortDescription(),
            $parser->getLongDescription()
        ]));
<commit_msg>Fixing a redundant condition.<commit_after><?php
namespace Mill;

use gossi\docblock\Docblock;
use gossi\docblock\tags\UnknownTag;
use Mill\Exceptions\Resource\UnsupportedDecoratorException;
use Mill\Parser\Annotation;
use Mill\Parser\MSON;
use Mill\Parser\Version;
use ReflectionClass;

/**
 * Class for tokenizing a docblock on a given class or method.
 *
 */
class Parser
{
    const REGEX_DECORATOR = '/^(?P<decorator>(:\w+)+)?/u';

    /**
     * The current class that we're going to be parsing.
     *
     * @var string
     */
    protected $class;

    /**
     * The current class method that we're parsing. Used to give better error messaging.
     *
     * @var null|string
     */
    protected $method;

    /**
     * @param string $class
     */
    public function __construct(string $class)
    {
        $this->class = $class;
    }

    /**
     * Get an array of HTTP (GET, POST, PUT, PATCH, DELETE) methods that are implemented on the current class.
     *
     * @return array
     */
    public function getHttpMethods()
    {
        $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];

        $reflection = new ReflectionClass($this->class);

        $valid_methods = [];
        foreach ($methods as $method) {
            if ($reflection->hasMethod($method)) {
                $valid_methods[] = $method;
            }
        }

        return $valid_methods;
    }

    /**
     * Locate, and parse, the annotations for a class or method.
     *
     * @param null|string $method_name
     * @return array An array containing all the found annotations.
     */
    public function getAnnotations(string $method_name = null): array
    {
        if (!empty($method_name)) {
            $this->method = $method_name;
        }

        $reader = Container::getAnnotationReader();
        $comments = $reader($this->class, $method_name);

        if (empty($comments)) {
            return [];
        }

        return $this->parseDocblock($comments);
    }

    /**
     * Parse a docblock comment into its parts.
     *
     * @link https://github.com/facebook/libphutil/blob/master/src/parser/docblock/PhutilDocblockParser.php
     * @param string $docblock
     * @param boolean $parse_description If we want to parse out an unstructured `description` annotation.
     * @return array Array of parsed annotations.
     */
    protected function parseDocblock(string $docblock, bool $parse_description = true): array
    {
        $original_docblock = $docblock;
        $annotations = [];
        $annotation_tags = [];
        $matches = null;

        $parser = self::getAnnotationsFromDocblock($docblock);
        $tags = $parser->getTags();

        /** @var UnknownTag $tag */
        foreach ($tags as $tag) {
            // If this isn't a Mill annotation, then ignore it.
            $annotation = $tag->getTagName();
            if (substr($annotation, 0, 4) !== 'api-') {
                continue;
            }

            $annotation_tags[] = $tag;
        }

        if (!empty($annotation_tags)) {
            $annotations = $this->parseAnnotations($annotation_tags, $original_docblock);
        }

        // Only parse out a `description` annotation if we need to (like in the instance of not parsing a
        // representation).
        if (!$parse_description) {
            return $annotations;
        }

        // Reconstruct the description as the developer wrote it.
        $description = implode("\n\n", array_filter([
            $parser->getShortDescription(),
            $parser->getLongDescription()
        ]));

{'fraction_non_alphanumeric': 0.12247457154800055, 'fraction_numerical': 0.0005573359342343597, 'mean_word_length': 2.133129637712789, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3253445', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1958, 'n_words': 549}