<commit_before><?php

namespace PHPSpec2\Matcher;

use PHPSpec2\Looper\Looper;
use PHPSpec2\Formatter\Presenter\PresenterInterface;
use PHPSpec2\Exception\Example\MatcherException;
use PHPSpec2\Exception\Example\FailureException;
use PHPSpec2\Exception\Example\NotEqualException;
use PHPSpec2\Wrapper\ArgumentsUnwrapper;

class ThrowMatcher implements MatcherInterface
{
    private $unwrapper;
    private $presenter;

    public function __construct(ArgumentsUnwrapper $unwrapper, PresenterInterface $presenter)
    {
        $this->unwrapper = $unwrapper;
        $this->presenter = $presenter;
    }

    public function supports($name, $subject, array $arguments)
    {
        return 'throw' === $name;
    }

    public function positiveMatch($name, $subject, array $arguments)
    {
        return $this->getLooper(array($this, 'verifyPositive'), $subject, $arguments);
    }

    public function negativeMatch($name, $subject, array $arguments)
    {
        return $this->getLooper(array($this, 'verifyNegative'), $subject, $arguments);
    }

    public function verifyPositive($callable, array $arguments, $class = null, $message = null)
    {
        try {
            call_user_func_array($callable, $arguments);
        } catch (\Exception $e) {
            if (null === $class) {
                return;
            }

            if (!$e instanceof $class) {
                throw new FailureException(sprintf(
                    'Expected exception of class %s, but got %s.',
                    $this->presenter->presentString($class),
                    $this->presenter->presentValue($e)
                ));
            }

            if (null !== $message && $e->getMessage() !== $message) {
                throw new NotEqualException(sprintf(
                    'Expected exception message %s, but got %s.',
                    $this->presenter->presentValue($message),
                    $this->presenter->presentValue($e->getMessage())
                ), $message, $e->getMessage());
            }

            return;
        }

        throw new FailureException('Expected to get exception, none got.');
    }

    public function verifyNegative($callable, array $arguments, $class = null, $message = null)
    {
        try {
            call_user_func_array($callable, $arguments);
        } catch (\Exception $e) {
            if (null === $class) {
                throw new FailureException(sprintf(
                    'Expected to not throw any exceptions, but got %s.',
                    $this->presenter->presentValue($e)
                ));
            }

            if ($e instanceof $class && null === $message) {
                throw new FailureException(sprintf(
                    'Expected to not throw %s exception, but got it.',
                    $this->presenter->presentString($class)
                ));
            }

            if ($e instanceof $class && $e->getMessage() === $message) {
                throw new FailureException(sprintf(
                    "Expected to not throw %s exception\n".
                    "with %s message,\nbut got it.",
                    $this->presenter->presentString($class),
                    $this->presenter->presentValue($message)
                ));
            }
        }
    }

    private function getLooper($check, $subject, array $arguments)
    {
        list($class, $message) = $this->getExceptionInformation($arguments);
        $unwrapper = $this->unwrapper;

        return new Looper(
            function($method, $arguments) use($check, $subject, $class, $message, $unwrapper) {
                $arguments = $unwrapper->unwrapAll($arguments);

                if (preg_match('/^during(.+)$/', $method, $matches)) {
                    $callable = lcfirst($matches[1]);
                } elseif (isset($arguments[0])) {
                    $callable  = $arguments[0];
                    $arguments = $arguments[1];
                } else {
                    throw new MatcherException('Provide callable to be checked for throwing.');
                }<commit_msg>Fix issue when using during with only method name as parameter<commit_after><?php

namespace PHPSpec2\Matcher;

use PHPSpec2\Looper\Looper;
use PHPSpec2\Formatter\Presenter\PresenterInterface;
use PHPSpec2\Exception\Example\MatcherException;
use PHPSpec2\Exception\Example\FailureException;
use PHPSpec2\Exception\Example\NotEqualException;
use PHPSpec2\Wrapper\ArgumentsUnwrapper;

class ThrowMatcher implements MatcherInterface
{
    private $unwrapper;
    private $presenter;

    public function __construct(ArgumentsUnwrapper $unwrapper, PresenterInterface $presenter)
    {
        $this->unwrapper = $unwrapper;
        $this->presenter = $presenter;
    }

    public function supports($name, $subject, array $arguments)
    {
        return 'throw' === $name;
    }

    public function positiveMatch($name, $subject, array $arguments)
    {
        return $this->getLooper(array($this, 'verifyPositive'), $subject, $arguments);
    }

    public function negativeMatch($name, $subject, array $arguments)
    {
        return $this->getLooper(array($this, 'verifyNegative'), $subject, $arguments);
    }

    public function verifyPositive($callable, array $arguments, $class = null, $message = null)
    {
        try {
            call_user_func_array($callable, $arguments);
        } catch (\Exception $e) {
            if (null === $class) {
                return;
            }

            if (!$e instanceof $class) {
                throw new FailureException(sprintf(
                    'Expected exception of class %s, but got %s.',
                    $this->presenter->presentString($class),
                    $this->presenter->presentValue($e)
                ));
            }

            if (null !== $message && $e->getMessage() !== $message) {
                throw new NotEqualException(sprintf(
                    'Expected exception message %s, but got %s.',
                    $this->presenter->presentValue($message),
                    $this->presenter->presentValue($e->getMessage())
                ), $message, $e->getMessage());
            }

            return;
        }

        throw new FailureException('Expected to get exception, none got.');
    }

    public function verifyNegative($callable, array $arguments, $class = null, $message = null)
    {
        try {
            call_user_func_array($callable, $arguments);
        } catch (\Exception $e) {
            if (null === $class) {
                throw new FailureException(sprintf(
                    'Expected to not throw any exceptions, but got %s.',
                    $this->presenter->presentValue($e)
                ));
            }

            if ($e instanceof $class && null === $message) {
                throw new FailureException(sprintf(
                    'Expected to not throw %s exception, but got it.',
                    $this->presenter->presentString($class)
                ));
            }

            if ($e instanceof $class && $e->getMessage() === $message) {
                throw new FailureException(sprintf(
                    "Expected to not throw %s exception\n".
                    "with %s message,\nbut got it.",
                    $this->presenter->presentString($class),
                    $this->presenter->presentValue($message)
                ));
            }
        }
    }

    private function getLooper($check, $subject, array $arguments)
    {
        list($class, $message) = $this->getExceptionInformation($arguments);
        $unwrapper = $this->unwrapper;

        return new Looper(
            function($method, $arguments) use($check, $subject, $class, $message, $unwrapper) {
                $arguments = $unwrapper->unwrapAll($arguments);

                if (preg_match('/^during(.+)$/', $method, $matches)) {
                    $callable = lcfirst($matches[1]);
                } elseif (isset($arguments[0])) {
                    $callable  = $arguments[0];
                    $arguments = isset($arguments[1]) ? $arguments[1] : array();
                } else {
                    throw new MatcherException('Provide callable to be checked for throwing.');
                }
{'fraction_non_alphanumeric': 0.12360774818401937, 'fraction_numerical': 0.002784503631961259, 'mean_word_length': 1.9221789883268483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '959280', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2070, 'n_words': 502}