<commit_before><?php
/**
 * This file is part of the Composite Utils package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the
 * LICENSE.md file that was distributed with this source code.
 *
 * @package spaark/composite-utils
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */

namespace Spaark\CompositeUtils\Test\Factory;

use Spaark\CompositeUtils\Factory\Reflection\TypeParser;
use PHPUnit\Framework\TestCase;
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty;
use Spaark\CompositeUtils\Model\Reflection\Type\StringType;
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType;
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
use Spaark\CompositeUtils\Service\RawPropertyAccessor;

/**
 *
 */
class TypeParserTest extends TestCase
{
    /**
     * @dataProvider superTypeProvider
     */
    public function testSuperType($type, $class, $nonNull)
    {
        $parser = new TypeParser();

        $nonNullType = $parser->parse($type);
        $access = new RawPropertyAccessor($nonNullType);
        $this->assertInstanceOf($class, $nonNullType);
        $this->assertSame($nonNull, $access->getRawValue('nullable'));

        $collectionType = $parser->parse($type . '[]');
        $access = new RawPropertyAccessor($collectionType);
        $this->assertInstanceOf(CollectionType::class, $collectionType);
        $this->assertInstanceOf($class, $access->getRawValue('of'));

        $nullType = $parser->parse('?' . $type);
        $access = new RawPropertyAccessor($nullType);
        $this->assertInstanceOf($class, $nullType);
        $this->assertTrue($access->getRawValue('nullable'));
    }

    public function testGeneric()
    {
        $parser = new TypeParser();

        $item = $parser->parse('Pair<string, int[]>');
        $this->assertInstanceOf(ObjectType::class, $item);

        $generics = $item->generics;
        $this->assertEquals(2, $generics->size());
        $this->assertInstanceOf(StringType::class, $generics[0]);
        $this->assertInstanceOf(CollectionType::class, $generics[1]);
        $this->assertInstanceOf(IntegerType::class, $generics[1]->of);
    }
<commit_msg>Add test of TypeParser with context<commit_after><?php
/**
 * This file is part of the Composite Utils package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the
 * LICENSE.md file that was distributed with this source code.
 *
 * @package spaark/composite-utils
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */

namespace Spaark\CompositeUtils\Test\Factory;

use Spaark\CompositeUtils\Factory\Reflection\TypeParser;
use PHPUnit\Framework\TestCase;
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty;
use Spaark\CompositeUtils\Model\Reflection\Type\StringType;
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType;
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
use Spaark\CompositeUtils\Model\Reflection\NamespaceBlock;
use Spaark\CompositeUtils\Model\Reflection\UseStatement;
use Spaark\CompositeUtils\Service\RawPropertyAccessor;

/**
 *
 */
class TypeParserTest extends TestCase
{
    /**
     * @dataProvider superTypeProvider
     */
    public function testSuperType($type, $class, $nonNull)
    {
        $parser = new TypeParser();

        $nonNullType = $parser->parse($type);
        $access = new RawPropertyAccessor($nonNullType);
        $this->assertInstanceOf($class, $nonNullType);
        $this->assertSame($nonNull, $access->getRawValue('nullable'));

        $collectionType = $parser->parse($type . '[]');
        $access = new RawPropertyAccessor($collectionType);
        $this->assertInstanceOf(CollectionType::class, $collectionType);
        $this->assertInstanceOf($class, $access->getRawValue('of'));

        $nullType = $parser->parse('?' . $type);
        $access = new RawPropertyAccessor($nullType);
        $this->assertInstanceOf($class, $nullType);
        $this->assertTrue($access->getRawValue('nullable'));
    }

    public function testGeneric()
    {
        $parser = new TypeParser();

        $item = $parser->parse('Pair<string, int[]>');
        $this->assertInstanceOf(ObjectType::class, $item);

        $generics = $item->generics;
        $this->assertEquals(2, $generics->size());
        $this->assertInstanceOf(StringType::class, $generics[0]);
        $this->assertInstanceOf(CollectionType::class, $generics[1]);
        $this->assertInstanceOf(IntegerType::class, $generics[1]->of);
    }

    public function testParserWithContext()
    {
        $reflectionComposite = new ReflectionComposite();
        $accessor = new RawPropertyAccessor($reflectionComposite);
        $namespace = new NamespaceBlock('');
        $accessor->setRawValue('namespace', $namespace);
        $namespace->useStatements['class'] =
            new UseStatement('full\class', 'class');
        $parser = new TypeParser($reflectionComposite);

        $type = $parser->parse('class');
        $this->assertInstanceOf(ObjectType::class, $type);
        $this->assertSame('full\class', $type->classname);
    }

{'fraction_non_alphanumeric': 0.14168451106352606, 'fraction_numerical': 0.0014275517487508922, 'mean_word_length': 4.072398190045249, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 65, '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': '340743', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1597, 'n_words': 286}