Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/MethodAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Murtukov\PHPCodeGenerator;

class MethodAbstract extends AbstractFunction implements BlockInterface
{
use ScopedContentTrait;
use DocBlockTrait;

final public function __construct(string $name, string $modifier = Modifier::PUBLIC, string $returnType = '')
{
$this->signature = new Signature($name, "$modifier abstract", $returnType);
$this->dependencyAwareChildren = [$this->signature];
}

/**
* @return static
*/
public static function new(string $name, string $modifier = Modifier::PUBLIC, string $returnType = ''): self
{
return new static($name, $modifier, $returnType);
}

public function generate(): string
{
return $this->buildDocBlock().$this->signature->generate(false).';';
}

}
165 changes: 165 additions & 0 deletions tests/MethodAbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

use Murtukov\PHPCodeGenerator\Argument;
use Murtukov\PHPCodeGenerator\Collection;
use Murtukov\PHPCodeGenerator\MethodAbstract;
use Murtukov\PHPCodeGenerator\Modifier;
use PHPStan\Testing\TestCase;

class MethodAbstractTest extends TestCase
{

/**
* @test
*/
public function emptyBase(): MethodAbstract
{
$method = MethodAbstract::new('myMethod', Modifier::PROTECTED, 'void');

$this->expectOutputString('protected abstract function myMethod(): void;');

echo $method;

return $method;
}

/**
* @test
* @depends emptyBase
*/
public function addArguments(MethodAbstract $method): MethodAbstract
{
$arg1 = $method->createArgument('arg1', SplHeap::class, null)->setNullable();

$arg2 = $method->createArgument('arg2', 'string', '');
$arg2->setByReference();

$method->add(Argument::new('arg3'));
$method->addArguments('arg4', 'arg5');

$this->assertEquals($arg1, $method->getArgument(1));
$this->assertEquals($arg2, $method->getArgument(2));

$this->expectOutputString(
'protected abstract function myMethod(?SplHeap $arg1 = null, string &$arg2 = \'\', $arg3, $arg4, $arg5): void;'
);

echo $method;

return $method;
}

/**
* @test
* @depends addArguments
*/
public function modifyParts(MethodAbstract $method): MethodAbstract
{
$method->setStatic();
$method->setReturnType(Collection::class);
$method->setDocBlock(<<<'DOCBLOCK'
Another simple function

@param SqlHeap|null $arg1
@param string $arg2
@param mixed $arg3
DOCBLOCK);

/** @var Argument $arg */
$arg = $method->getArgument(5);
$arg->setSpread();

$this->assertEquals(true, $method->isStatic());
$this->assertEquals('Collection', $method->getReturnType());

$this->expectOutputString(<<<'CODE'
/**
* Another simple function
*
* @param SqlHeap|null $arg1
* @param string $arg2
* @param mixed $arg3
*/
protected abstract static function myMethod(?SplHeap $arg1 = null, string &$arg2 = '', $arg3, $arg4, ...$arg5): Collection;
CODE);

echo $method;

return $method;
}


/**
* @test
* @depends modifyParts
*/
public function removeParts(MethodAbstract $method): void
{
$method->removeArgument(1);
$method->removeArgument(2);
$method->removeArgument(3);
$method->unsetStatic();
$method->clearContent();
$method->removeDocBlock();

$this->expectOutputString(<<<'CODE'
protected abstract function myMethod($arg4, ...$arg5): Collection;
CODE);

echo $method;
}

/**
* @test
*/
public function withPromotedArguments(): MethodAbstract
{
$method = MethodAbstract::new('__construct');

$method->addArgument('firstName', 'string', 'Alex', Modifier::PRIVATE);
$method->addArgument('lastName', 'string', 'Kowalski', Modifier::PRIVATE);
$method->addArgument('age', 'int', Argument::NO_PARAM, Modifier::PRIVATE);

$method->signature->setMultiline();

echo $method;

$this->expectOutputString(<<<'CODE'
public abstract function __construct(
private string $firstName = 'Alex',
private string $lastName = 'Kowalski',
private int $age
);
CODE);

return $method;
}


/**
* @test
* @depends withPromotedArguments
*/
public function addNormalArguments(MethodAbstract $method): MethodAbstract
{
$method->addArgument('isStudent', 'bool');
$method->addArgument('isEmployed');

echo $method;

$this->expectOutputString(<<<'CODE'
public abstract function __construct(
private string $firstName = 'Alex',
private string $lastName = 'Kowalski',
private int $age,
bool $isStudent,
$isEmployed
);
CODE);

return $method;
}

}