<commit_before><?php

/*
 +--------------------------------------------------------------------------+
 | Zephir                                                                   |
 | Copyright (c) 2013-present Zephir (https://zephir-lang.com/)             |
 |                                                                          |
 | This source file is subject the MIT license, that is bundled with this   |
 | package in the file LICENSE, and is available through the world-wide-web |
 | at the following url: http://zephir-lang.com/license.html                |
 +--------------------------------------------------------------------------+
 */

namespace Zephir\Commands;

use ReflectionClass;
use SplObjectStorage;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Zephir\Exception\ValidationException;
use Zephir\Exception\OutOfBoundsException;
use Zephir\Exception\BadMethodCallException;
use Zephir\Exception\InvalidArgumentException;

/**
 * Zephir\Commands\Manager
 *
 * @package Zephir\Commands
 */
class Manager extends SplObjectStorage
{
    private $similarSounds = [];

    /**
     * Registers builtin commands.
     *
     * @return void
     */
    public function registerBuiltinCommands()
    {
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__)));
        $iterator->rewind();

        while($iterator->valid()) {
            $fileInfo = $iterator->current();
            if ($fileInfo->isDir() || $fileInfo->getExtension() !== 'php') {
                $iterator->next();
                continue;
            }

            $className = __NAMESPACE__ . '\\' . $fileInfo->getBasename('.php');
            if ($className === __CLASS__) {
                $iterator->next();
                continue;
            }

            $command = new ReflectionClass($className);
            if ($command->isAbstract() || $command->isInterface()) {
                $iterator->next();
                continue;
            }

            if (!$command->implementsInterface(CommandInterface::class)) {
                $iterator->next();
                continue;
            }

            $this->attach($command->newInstance());
            $iterator->next();
        }
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface $object object whose identifier is to be calculated.
     * @return string
     * @throws ValidationException
     */
    public function getHash($object)
    {
        $this->validate($object);

        return $object->getCommand();
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface $object The command to add.
     * @param mixed $data
     * @throws ValidationException
     */
    public function attach($object, $data = null)
    {
        $this->validate($object);

        parent::attach($object, $data);

        $action = $this->getHash($object);
        $this->similarSounds[metaphone($action)] = $action;
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface $object The command to remove.
     * @return void
     * @throws ValidationException
     */
    public function detach($object)
    {
        $this->validate($object);

        parent::detach($object);

        $action = $this->getHash($object);
        unset($this->similarSounds[metaphone($action)]);<commit_msg>Pass Commands Manager to the each command<commit_after><?php

/*
 +--------------------------------------------------------------------------+
 | Zephir                                                                   |
 | Copyright (c) 2013-present Zephir (https://zephir-lang.com/)             |
 |                                                                          |
 | This source file is subject the MIT license, that is bundled with this   |
 | package in the file LICENSE, and is available through the world-wide-web |
 | at the following url: http://zephir-lang.com/license.html                |
 +--------------------------------------------------------------------------+
 */

namespace Zephir\Commands;

use ReflectionClass;
use SplObjectStorage;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Zephir\Exception\ValidationException;
use Zephir\Exception\OutOfBoundsException;
use Zephir\Exception\BadMethodCallException;

/**
 * Zephir\Commands\Manager
 *
 * @package Zephir\Commands
 */
class Manager extends SplObjectStorage
{
    private $similarSounds = [];

    /**
     * Registers builtin commands.
     *
     * @return void
     */
    public function registerBuiltinCommands()
    {
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__)));
        $iterator->rewind();

        while($iterator->valid()) {
            $fileInfo = $iterator->current();
            if ($fileInfo->isDir() || $fileInfo->getExtension() !== 'php') {
                $iterator->next();
                continue;
            }

            $className = __NAMESPACE__ . '\\' . $fileInfo->getBasename('.php');
            if ($className === __CLASS__) {
                $iterator->next();
                continue;
            }

            $command = new ReflectionClass($className);
            if ($command->isAbstract() || $command->isInterface()) {
                $iterator->next();
                continue;
            }

            if (!$command->implementsInterface(CommandInterface::class)) {
                $iterator->next();
                continue;
            }

            $this->attach($command->newInstanceArgs([$this]));
            $iterator->next();
        }
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface $object object whose identifier is to be calculated.
     * @return string
     * @throws ValidationException
     */
    public function getHash($object)
    {
        $this->validate($object);

        return $object->getCommand();
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface|object $object The command to add.
     * @param mixed $data
     * @throws ValidationException
     */
    public function attach($object, $data = null)
    {
        $this->validate($object);

        parent::attach($object, $data);

        $action = $this->getHash($object);
        $this->similarSounds[metaphone($action)] = $action;
    }

    /**
     * {@inheritdoc}
     *
     * @param CommandInterface|object $object The command to remove.
     * @return void
     * @throws ValidationException
     */
    public function detach($object)
    {
        $this->validate($object);

        parent::detach($object);

        $action = $this->getHash($object);
        unset($this->similarSounds[metaphone($action)]);
{'fraction_non_alphanumeric': 0.1659713945172825, 'fraction_numerical': 0.0011918951132300357, 'mean_word_length': 1.6992360273421794, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1048530', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1638, 'n_words': 349}