<commit_before><?php

namespace Prophecy\Doubler;

use Prophecy\Doubler\Generator\ClassMirror;
use Prophecy\Doubler\Generator\ClassCreator;
use Prophecy\Exception\InvalidArgumentException;

use ReflectionClass;

/*
 * This file is part of the Prophecy.
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Cached class doubler.
 * Prevents mirroring/creation of the same structure twice.
 *
 * @author <NAME> <<EMAIL>>
 */
class Doubler
{
    private $mirror;
    private $creator;
    private $namer;
    private $patches = array();

    /**
     * Initializes doubler.
     *
     * @param ClassMirror   $mirror
     * @param ClassCreator  $creator
     * @param NameGenerator $namer
     */
    public function __construct(ClassMirror $mirror = null, ClassCreator $creator = null,
                                NameGenerator $namer = null)
    {
        $this->mirror  = $mirror  ?: new ClassMirror;
        $this->creator = $creator ?: new ClassCreator;
        $this->namer   = $namer   ?: new NameGenerator;
    }

    /**
     * Returns list of registered class patches.
     *
     * @return array
     */
    public function getClassPatches()
    {
        return $this->patches;
    }

    /**
     * Registers new class patch.
     *
     * @param ClassPatch\ClassPatchInterface $patch
     */
    public function registerClassPatch(ClassPatch\ClassPatchInterface $patch)
    {
        $this->patches[] = $patch;

        @usort($this->patches, function($patch1, $patch2) {
            return $patch2->getPriority() - $patch1->getPriority();
        });
    }

    /**
     * Creates double from specific class or/and list of interfaces.
     *
     * @param ReflectionClass $class
     * @param array           $interfaces Array of ReflectionClass instances
     * @param array           $args       Constructor arguments
     *
     * @return DoubleInterface
     *
     * @throws \Prophecy\Exception\InvalidArgumentException
     */
    public function double(ReflectionClass $class = null, array $interfaces, array $args = null)
    {
        foreach ($interfaces as $interface) {
            if (!$interface instanceof ReflectionClass) {
                throw new InvalidArgumentException(sprintf(
                    "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n".
                    "a second argument to `Doubler::double(...)`, but got %s.",
                    is_object($interface) ? get_class($interface).' class' : gettype($interface)
                ));
            }
        }

        $classname  = $this->createDoubleClass($class, $interfaces);
        $reflection = new ReflectionClass($classname);

        if (null !== $args) {
            return $reflection->newInstanceWithArgs($args);
        }
        if ((null === $constructor = $reflection->getConstructor()) || $constructor->isPublic()) {
            return $reflection->newInstance();
        }
        if (version_compare(PHP_VERSION, '5.4', '<')) {
            return unserialize(sprintf('O:%d:"%s":0:{}', strlen($classname), $classname));
        }

        return $reflection->newInstanceWithoutConstructor();
    }
<commit_msg>Fixed double instantiation for constructor with arguments<commit_after><?php

namespace Prophecy\Doubler;

use Prophecy\Doubler\Generator\ClassMirror;
use Prophecy\Doubler\Generator\ClassCreator;
use Prophecy\Exception\InvalidArgumentException;

use ReflectionClass;

/*
 * This file is part of the Prophecy.
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Cached class doubler.
 * Prevents mirroring/creation of the same structure twice.
 *
 * @author <NAME> <<EMAIL>>
 */
class Doubler
{
    private $mirror;
    private $creator;
    private $namer;
    private $patches = array();

    /**
     * Initializes doubler.
     *
     * @param ClassMirror   $mirror
     * @param ClassCreator  $creator
     * @param NameGenerator $namer
     */
    public function __construct(ClassMirror $mirror = null, ClassCreator $creator = null,
                                NameGenerator $namer = null)
    {
        $this->mirror  = $mirror  ?: new ClassMirror;
        $this->creator = $creator ?: new ClassCreator;
        $this->namer   = $namer   ?: new NameGenerator;
    }

    /**
     * Returns list of registered class patches.
     *
     * @return array
     */
    public function getClassPatches()
    {
        return $this->patches;
    }

    /**
     * Registers new class patch.
     *
     * @param ClassPatch\ClassPatchInterface $patch
     */
    public function registerClassPatch(ClassPatch\ClassPatchInterface $patch)
    {
        $this->patches[] = $patch;

        @usort($this->patches, function($patch1, $patch2) {
            return $patch2->getPriority() - $patch1->getPriority();
        });
    }

    /**
     * Creates double from specific class or/and list of interfaces.
     *
     * @param ReflectionClass $class
     * @param array           $interfaces Array of ReflectionClass instances
     * @param array           $args       Constructor arguments
     *
     * @return DoubleInterface
     *
     * @throws \Prophecy\Exception\InvalidArgumentException
     */
    public function double(ReflectionClass $class = null, array $interfaces, array $args = null)
    {
        foreach ($interfaces as $interface) {
            if (!$interface instanceof ReflectionClass) {
                throw new InvalidArgumentException(sprintf(
                    "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n".
                    "a second argument to `Doubler::double(...)`, but got %s.",
                    is_object($interface) ? get_class($interface).' class' : gettype($interface)
                ));
            }
        }

        $classname  = $this->createDoubleClass($class, $interfaces);
        $reflection = new ReflectionClass($classname);

        if (null !== $args) {
            return $reflection->newInstanceArgs($args);
        }
        if ((null === $constructor = $reflection->getConstructor()) || $constructor->isPublic()) {
            return $reflection->newInstance();
        }
        if (version_compare(PHP_VERSION, '5.4', '<')) {
            return unserialize(sprintf('O:%d:"%s":0:{}', strlen($classname), $classname));
        }

        return $reflection->newInstanceWithoutConstructor();
    }

{'fraction_non_alphanumeric': 0.1210843373493976, 'fraction_numerical': 0.002710843373493976, 'mean_word_length': 2.372778059928898, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 49, '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': '4311084', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1822, 'n_words': 460}