<commit_before><?php

namespace Codeception\Util;

class Stub
{
    public static $magicMethods = ['__isset', '__get', '__set'];

    /**
     * Instantiates a class without executing a constructor.
     * Properties and methods can be set as a second parameter.
     * Even protected and private properties can be set.
     *
     * ``` php
     * <?php
     * Stub::make('User');
     * Stub::make('User', array('name' => 'davert));
     * ?>
     * ```
     *
     * Accepts either name of class or object of that class
     *
     * ``` php
     * <?php
     * Stub::make(new User, array('name' => 'davert));
     * ?>
     * ```
     *
     * To replace method provide it's name as a key in second parameter and it's return value or callback function as parameter
     *
     * ``` php
     * <?php
     * Stub::make('User', array('save' => function () { return true; }));
     * Stub::make('User', array('save' => true }));
     * ?>
     * ```
     *
     * @param                                  $class - A class to be mocked
     * @param array                            $params - properties and methods to set
     * @param bool|\PHPUnit_Framework_TestCase $testCase
     *
     * @return object - mock
     * @throws \RuntimeException when class not exists
     */
    public static function make($class, $params = [], $testCase = false)
    {
        $class = self::getClassname($class);
        if (!class_exists($class)) {
            throw new \RuntimeException("Stubbed class $class doesn't exist.");
        }

        $reflection = new \ReflectionClass($class);
        $callables  = self::getMethodsToReplace($reflection, $params);
        if ($reflection->isAbstract()) {
            $arguments = empty($callables) ? [] : array_keys($callables);
            $mock      = self::generateMockForAbstractClass($class, $arguments, '', false, $testCase);
        } else {
            $arguments = empty($callables) ? null : array_keys($callables);
            $mock      = self::generateMock($class, $arguments, [], '', false, $testCase);
        }

        self::bindParameters($mock, $params);
        //only set __mocked if __set is not implemented
        //to avoid __set implementations that error on overloading
        if (!$reflection->hasMethod('__set')) {
            $mock->__mocked = $class;
        }

        return $mock;
    }

    /**
     *
     * Works exactly the same as Stub::make, but doesn't fail when the mocked class implements __set
     * In some extensions, the __set method is implemented and triggers errors when an undefined property is set
     * Stub::makeNative checks the errors, and will return the mocked object regardless
     *
     * ``` php
     * <?php
     * Stub::makeNative('\CouchbaseBucket');
     * ?>
     * ```
     * @param                                  $class - A class to be mocked
     * @param array                            $params - properties and methods to set
     * @param bool|\PHPUnit_Framework_TestCase $testCase
     *
     * @return object - mock
     * @throws \RuntimeException when class not exists
     */
    public static function makeNative($class, $params = [], $testCase = false)
    {
        $class = self::getClassname($class);
        if (!class_exists($class)) {
            throw new \RuntimeException(
                sprintf(
                    'Stubbed extension class %s does not exist',
                    $class
                )
            );
        }
        $reflection = new \ReflectionClass($class);
        $callables  = self::getMethodsToReplace($reflection, $params);
        if ($reflection->isAbstract()) {
            $arguments = empty($callables) ? [] : array_keys($callables);
            $mock      = self::generateMockForAbstractClass($class, $arguments, '', false, $testCase);
        } else {
            $arguments = empty($callables) ? null : array_keys($callables);
            $mock = self::generateMock($class, $arguments, [], '', false, $testCase);
        }

        self::bindParameters($mock, $params);
        try {
            $mock->__mocked = $class;
        } catch (\PHPUnit_Framework_Exception $e) {
            if (
                strstr($e->getMessage(), 'property via __set(): __mocked') === false
                ||
                strstr($e->getTraceAsString(), 'Native]') === false
            ) {
                throw $e;
            }
        }

        return $mock;

    }

    /**
     * Creates $num instances of class through `Stub::make`.
     *
     * @param       $class
     * @param int   $num
     * @param array $params
     *
     * @return array
     */
    public static function factory($class, $num = 1, $params = [])
    {
        $objects = [];
        for ($i = 0; $i < $num; $i++) {
            $objects[] = self::make($class, $params);
        }

        return $objects;
    }

    /**
     * Instantiates class having all methods replaced with dummies except one.
     * Constructor is not triggered.
     * Properties and methods can be replaced.
     * Even protected and private properties can be set.<commit_msg>Remove makeNative, seeing as we no longer need it<commit_after><?php

namespace Codeception\Util;

class Stub
{
    public static $magicMethods = ['__isset', '__get', '__set'];

    /**
     * Instantiates a class without executing a constructor.
     * Properties and methods can be set as a second parameter.
     * Even protected and private properties can be set.
     *
     * ``` php
     * <?php
     * Stub::make('User');
     * Stub::make('User', array('name' => 'davert));
     * ?>
     * ```
     *
     * Accepts either name of class or object of that class
     *
     * ``` php
     * <?php
     * Stub::make(new User, array('name' => 'davert));
     * ?>
     * ```
     *
     * To replace method provide it's name as a key in second parameter and it's return value or callback function as parameter
     *
     * ``` php
     * <?php
     * Stub::make('User', array('save' => function () { return true; }));
     * Stub::make('User', array('save' => true }));
     * ?>
     * ```
     *
     * @param                                  $class - A class to be mocked
     * @param array                            $params - properties and methods to set
     * @param bool|\PHPUnit_Framework_TestCase $testCase
     *
     * @return object - mock
     * @throws \RuntimeException when class not exists
     */
    public static function make($class, $params = [], $testCase = false)
    {
        $class = self::getClassname($class);
        if (!class_exists($class)) {
            throw new \RuntimeException("Stubbed class $class doesn't exist.");
        }

        $reflection = new \ReflectionClass($class);
        $callables  = self::getMethodsToReplace($reflection, $params);
        if ($reflection->isAbstract()) {
            $arguments = empty($callables) ? [] : array_keys($callables);
            $mock      = self::generateMockForAbstractClass($class, $arguments, '', false, $testCase);
        } else {
            $arguments = empty($callables) ? null : array_keys($callables);
            $mock      = self::generateMock($class, $arguments, [], '', false, $testCase);
        }

        self::bindParameters($mock, $params);
        //only set __mocked if __set is not implemented
        //to avoid __set implementations that error on overloading
        if (!$reflection->hasMethod('__set')) {
            $mock->__mocked = $class;
        }

        return $mock;
    }

    /**
     * Creates $num instances of class through `Stub::make`.
     *
     * @param       $class
     * @param int   $num
     * @param array $params
     *
     * @return array
     */
    public static function factory($class, $num = 1, $params = [])
    {
        $objects = [];
        for ($i = 0; $i < $num; $i++) {
            $objects[] = self::make($class, $params);
        }

        return $objects;
    }

    /**
     * Instantiates class having all methods replaced with dummies except one.
     * Constructor is not triggered.
     * Properties and methods can be replaced.
     * Even protected and private properties can be set.
{'fraction_non_alphanumeric': 0.13298198308616252, 'fraction_numerical': 0.0004902561588429955, 'mean_word_length': 1.996694821887624, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3283908', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2213, 'n_words': 647}