<commit_before><?php
/**
 * teqneers/ext-direct
 *
 * @category   TQ
 * @package    TQ\ExtDirect
 * @copyright  Copyright (C) 2015 by TEQneers GmbH & Co. KG
 */

namespace TQ\ExtDirect\Metadata\Driver;

use Doctrine\Common\Annotations\Reader;
use Metadata\Driver\AdvancedDriverInterface;
use Metadata\NullMetadata;
use TQ\ExtDirect\Annotation\Parameter;
use TQ\ExtDirect\Metadata\ActionMetadata;
use TQ\ExtDirect\Metadata\MethodMetadata;

/**
 * Class AnnotationDriver
 *
 * @package TQ\ExtDirect\Metadata\Driver
 */
class AnnotationDriver implements AdvancedDriverInterface
{
    const ACTION_ANNOTATION_CLASS = 'TQ\ExtDirect\Annotation\Action';
    const METHOD_ANNOTATION_CLASS = 'TQ\ExtDirect\Annotation\Method';

    /**
     * @var Reader
     */
    private $reader;

    /**
     * @var array
     */
    protected $paths = array();

    /**
     * @var array|null
     */
    protected $classNames;

    /**
     * @param Reader            $reader
     * @param array|string|null $paths
     */
    public function __construct(Reader $reader, $paths = null)
    {
        $this->reader = $reader;
        if ($paths) {
            $this->addPaths((array)$paths);
        }
    }

    /**
     * @param array $paths
     */
    protected function addPaths(array $paths)
    {
        $this->paths = array_unique(array_merge($this->paths, $paths));
    }

    /**
     * {@inheritdoc}
     */
    public function loadMetadataForClass(\ReflectionClass $class)
    {
        $actionMetadata = new ActionMetadata($class->getName());

        $actionMetadata->fileResources[] = $class->getFilename();

        $actionAnnotation = $this->reader->getClassAnnotation($class, self::ACTION_ANNOTATION_CLASS);

        /** @var \TQ\ExtDirect\Annotation\Action $actionAnnotation */
        if ($actionAnnotation !== null) {
            $actionMetadata->isAction  = true;
            $actionMetadata->serviceId = $actionAnnotation->serviceId;
        } else {
            return new NullMetadata($class->getName());
        }

        $methodCount = 0;
        foreach ($class->getMethods() as $reflectionMethod) {
            $methodMetadata   = new MethodMetadata($class->getName(), $reflectionMethod->getName());
            $methodAnnotation = $this->reader->getMethodAnnotation($reflectionMethod, self::METHOD_ANNOTATION_CLASS);

            /** @var \TQ\ExtDirect\Annotation\Method $methodAnnotation */
            if ($methodAnnotation !== null) {
                $methodMetadata->isMethod      = true;
                $methodMetadata->isFormHandler = $methodAnnotation->formHandler;
                $methodMetadata->addParameters($reflectionMethod->getParameters());

                foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) {
                    if ($annotation instanceof Parameter) {
                        if (!empty($annotation->constraints)) {
                            $methodMetadata->addParameterConstraints(
                                $annotation->name,
                                $annotation->constraints
                            );
                        }
                    }
                }

                $actionMetadata->addMethodMetadata($methodMetadata);
                $methodCount++;
            }
        }

        if ($methodCount < 1) {
            return new NullMetadata($class->getName());
        }

        return $actionMetadata;
    }

    /**
     * {@inheritdoc}
     */
    public function getAllClassNames()
    {
        if ($this->classNames !== null) {
            return $this->classNames;
        }

        $classes       = array();
        $includedFiles = array();
        foreach ($this->paths as $path) {
            if (!is_dir($path)) {
                throw new \RuntimeException('"' . $path . '" is not a valid directory');
            }

            $iterator = new \RegexIterator(
                new \RecursiveIteratorIterator(
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
                    \RecursiveIteratorIterator::LEAVES_ONLY
                ),
                '/^.+\.php$/i',
                \RecursiveRegexIterator::GET_MATCH
            );

            foreach ($iterator as $file) {
                $sourceFile = realpath($file[0]);
                require_once $sourceFile;
                $includedFiles[] = $sourceFile;
            }
        }

        $declared = get_declared_classes();
        foreach ($declared as $className) {
            $rc         = new \ReflectionClass($className);
            $sourceFile = $rc->getFileName();
            if (in_array($sourceFile, $includedFiles) && !$this->isActionClass($className)) {
                $classes[] = $className;
            }
        }

        $this->classNames = $classes;
        return $classes;
    }

    /**
     * @param string $className
     * @return boolean
     */
    protected function isActionClass($className)
    {
        $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className));

        foreach ($classAnnotations as $annotation) {
            if (get_class($annotation) === self::ACTION_ANNOTATION_CLASS) {
                return false;
            }
        }
        return true;
    }
}<commit_msg>adapted logic to method name<commit_after><?php
/**
 * teqneers/ext-direct
 *
 * @category   TQ
 * @package    TQ\ExtDirect
 * @copyright  Copyright (C) 2015 by TEQneers GmbH & Co. KG
 */

namespace TQ\ExtDirect\Metadata\Driver;

use Doctrine\Common\Annotations\Reader;
use Metadata\Driver\AdvancedDriverInterface;
use Metadata\NullMetadata;
use TQ\ExtDirect\Annotation\Parameter;
use TQ\ExtDirect\Metadata\ActionMetadata;
use TQ\ExtDirect\Metadata\MethodMetadata;

/**
 * Class AnnotationDriver
 *
 * @package TQ\ExtDirect\Metadata\Driver
 */
class AnnotationDriver implements AdvancedDriverInterface
{
    const ACTION_ANNOTATION_CLASS = 'TQ\ExtDirect\Annotation\Action';
    const METHOD_ANNOTATION_CLASS = 'TQ\ExtDirect\Annotation\Method';

    /**
     * @var Reader
     */
    private $reader;

    /**
     * @var array
     */
    protected $paths = array();

    /**
     * @var array|null
     */
    protected $classNames;

    /**
     * @param Reader            $reader
     * @param array|string|null $paths
     */
    public function __construct(Reader $reader, $paths = null)
    {
        $this->reader = $reader;
        if ($paths) {
            $this->addPaths((array)$paths);
        }
    }

    /**
     * @param array $paths
     */
    protected function addPaths(array $paths)
    {
        $this->paths = array_unique(array_merge($this->paths, $paths));
    }

    /**
     * {@inheritdoc}
     */
    public function loadMetadataForClass(\ReflectionClass $class)
    {
        $actionMetadata = new ActionMetadata($class->getName());

        $actionMetadata->fileResources[] = $class->getFilename();

        $actionAnnotation = $this->reader->getClassAnnotation($class, self::ACTION_ANNOTATION_CLASS);

        /** @var \TQ\ExtDirect\Annotation\Action $actionAnnotation */
        if ($actionAnnotation !== null) {
            $actionMetadata->isAction  = true;
            $actionMetadata->serviceId = $actionAnnotation->serviceId;
        } else {
            return new NullMetadata($class->getName());
        }

        $methodCount = 0;
        foreach ($class->getMethods() as $reflectionMethod) {
            $methodMetadata   = new MethodMetadata($class->getName(), $reflectionMethod->getName());
            $methodAnnotation = $this->reader->getMethodAnnotation($reflectionMethod, self::METHOD_ANNOTATION_CLASS);

            /** @var \TQ\ExtDirect\Annotation\Method $methodAnnotation */
            if ($methodAnnotation !== null) {
                $methodMetadata->isMethod      = true;
                $methodMetadata->isFormHandler = $methodAnnotation->formHandler;
                $methodMetadata->addParameters($reflectionMethod->getParameters());

                foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) {
                    if ($annotation instanceof Parameter) {
                        if (!empty($annotation->constraints)) {
                            $methodMetadata->addParameterConstraints(
                                $annotation->name,
                                $annotation->constraints
                            );
                        }
                    }
                }

                $actionMetadata->addMethodMetadata($methodMetadata);
                $methodCount++;
            }
        }

        if ($methodCount < 1) {
            return new NullMetadata($class->getName());
        }

        return $actionMetadata;
    }

    /**
     * {@inheritdoc}
     */
    public function getAllClassNames()
    {
        if ($this->classNames !== null) {
            return $this->classNames;
        }

        $classes       = array();
        $includedFiles = array();
        foreach ($this->paths as $path) {
            if (!is_dir($path)) {
                throw new \RuntimeException('"' . $path . '" is not a valid directory');
            }

            $iterator = new \RegexIterator(
                new \RecursiveIteratorIterator(
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
                    \RecursiveIteratorIterator::LEAVES_ONLY
                ),
                '/^.+\.php$/i',
                \RecursiveRegexIterator::GET_MATCH
            );

            foreach ($iterator as $file) {
                $sourceFile = realpath($file[0]);
                require_once $sourceFile;
                $includedFiles[] = $sourceFile;
            }
        }

        $declared = get_declared_classes();
        foreach ($declared as $className) {
            $rc         = new \ReflectionClass($className);
            $sourceFile = $rc->getFileName();
            if (in_array($sourceFile, $includedFiles) && $this->isActionClass($className)) {
                $classes[] = $className;
            }
        }

        $this->classNames = $classes;
        return $classes;
    }

    /**
     * @param string $className
     * @return boolean
     */
    protected function isActionClass($className)
    {
        $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className));

        foreach ($classAnnotations as $annotation) {
            if (get_class($annotation) === self::ACTION_ANNOTATION_CLASS) {
                return true;
            }
        }
        return false;
    }
}
{'fraction_non_alphanumeric': 0.12342070526117292, 'fraction_numerical': 0.0013200075429002452, 'mean_word_length': 1.9036408431426226, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 81, 'https://': 0, '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': '4139747', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2691, 'n_words': 502}