Skip to content

Commit

Permalink
Common update
Browse files Browse the repository at this point in the history
- Change PHP requirement
- Add PHPDoc blocks
- Add type-hints
- Other minor fixes
- Make constructors final
  • Loading branch information
murtukov committed Jan 12, 2021
1 parent 8a36063 commit 5beed9b
Show file tree
Hide file tree
Showing 47 changed files with 530 additions and 213 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.4",
"php": ">=7.4",
"ext-json": "*"
},
"autoload": {
Expand All @@ -20,11 +20,13 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.1"
"phpunit/phpunit": "^9.4.3",
"phpstan/phpstan": "^0.12.58"
},
"scripts": {
"test": "bin/phpunit --color=always -v --debug",
"install-cs": "test -f php-cs-fixer.phar || wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.3/php-cs-fixer.phar -O php-cs-fixer.phar",
"install-cs": "test -f php-cs-fixer.phar || wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.17.3/php-cs-fixer.phar -O php-cs-fixer.phar",
"static-analysis": "vendor/bin/phpstan analyse src tests",
"fix-cs": [
"@install-cs",
"@php php-cs-fixer.phar fix --diff -v --allow-risky=yes --ansi"
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 8
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
40 changes: 39 additions & 1 deletion src/AbstractFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function getReturnType(): string
return $this->signature->getReturnType();
}

/**
* @return $this
*/
public function setReturnType(string $returnType): self
{
$this->signature->setReturnType($returnType);
Expand All @@ -25,61 +28,90 @@ public function getArgument(int $index = 1): ?Argument
return $this->signature->getArgument($index);
}

/**
* @return $this
*/
public function removeArgument(int $index): self
{
$this->signature->removeArgument($index);

return $this;
}

/**
* @return $this
*/
public function removeArguments(): self
{
$this->signature->removeArguments();

return $this;
}

/**
* @param mixed $defaultValue
*/
public function createArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): Argument
{
return $this->signature->createArgument($name, $type, $defaultValue);
}

/**
* @param mixed $defaultValue
*
* @return $this
*/
public function addArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): self
{
$this->signature->addArgument($name, $type, $defaultValue);

return $this;
}

/**
* @return $this
*/
public function addArguments(string ...$names): self
{
$this->signature->addArguments(...$names);

return $this;
}

/**
* @return $this
*/
public function add(FunctionMemberInterface $member): self
{
$this->signature->add($member);

return $this;
}

/**
* @return $this
*/
public function bindVar(string $name, bool $isByReference = false): self
{
$this->signature->bindVar($name, $isByReference);

return $this;
}

/**
* @return $this
*/
public function bindVars(string ...$names): self
{
$this->signature->bindVars(...$names);

return $this;
}

public function removeBindVars()
/**
* @return $this
*/
public function removeBindVars(): self
{
$this->signature->removeBindVars();

Expand All @@ -91,13 +123,19 @@ public function isStatic(): bool
return $this->signature->isStatic;
}

/**
* @return $this
*/
public function setStatic(): self
{
$this->signature->isStatic = true;

return $this;
}

/**
* @return $this
*/
public function unsetStatic()
{
$this->signature->isStatic = false;
Expand Down
50 changes: 43 additions & 7 deletions src/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Argument extends DependencyAwareGenerator implements FunctionMemberInterfa
*/
public const NO_PARAM = INF;

private string $type;
private string $name;
private bool $isSpread = false;
private bool $isByReference = false;
private bool $isNullable = false;
private string $type;
private string $name;
private bool $isSpread = false;
private bool $isByReference = false;
private bool $isNullable = false;

/**
* @var mixed
Expand All @@ -26,8 +26,10 @@ class Argument extends DependencyAwareGenerator implements FunctionMemberInterfa
* Argument constructor.
*
* @param mixed $defaultValue
*
* @throws Exception\UnrecognizedValueTypeException
*/
public function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM)
public final function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM)
{
$this->name = $name;
$this->type = $this->resolveQualifier($type);
Expand All @@ -37,9 +39,15 @@ public function __construct(string $name, string $type = '', $defaultValue = sel
}
}

/**
* @param mixed $defaultValue
*
* @return static
* @throws Exception\UnrecognizedValueTypeException
*/
public static function new(string $name, string $type = '', $defaultValue = self::NO_PARAM): self
{
return new self($name, $type, $defaultValue);
return new static($name, $type, $defaultValue);
}

public function generate(): string
Expand Down Expand Up @@ -80,13 +88,19 @@ public function isSpread(): bool
return $this->isSpread;
}

/**
* @return $this
*/
public function setSpread(): self
{
$this->isSpread = true;

return $this;
}

/**
* @return $this
*/
public function unsetSpread(): self
{
$this->isSpread = false;
Expand All @@ -99,27 +113,43 @@ public function isByReference(): bool
return $this->isByReference;
}

/**
* @return $this
*/
public function setByReference(): self
{
$this->isByReference = true;

return $this;
}

/**
* @return $this
*/
public function unsetByReference(): self
{
$this->isByReference = false;

return $this;
}

/**
* @return $this
*/
public function setType(string $type): self
{
$this->type = $type;

return $this;
}

/**
* @param mixed $value
*
* @return $this
*
* @throws Exception\UnrecognizedValueTypeException
*/
public function setDefaultValue($value): self
{
if (INF !== $value) {
Expand All @@ -131,13 +161,19 @@ public function setDefaultValue($value): self
return $this;
}

/**
* @return $this
*/
public function unsetNullable(): self
{
$this->isNullable = false;

return $this;
}

/**
* @return $this
*/
public function setNullable(): self
{
$this->isNullable = true;
Expand Down
21 changes: 18 additions & 3 deletions src/ArrowFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@ class ArrowFunction extends AbstractFunction
/** @var GeneratorInterface|string */
private $expression;

public function __construct($expression = null, string $returnType = '')
/**
* @param mixed $expression
*/
public final function __construct($expression = null, string $returnType = '')
{
$this->signature = new Signature('', Modifier::NONE, $returnType, 'fn');
$this->expression = $this->manageExprDependency($expression);

$this->dependencyAwareChildren[] = $this->signature;
}

public static function new($expression = null, string $returnType = '')
/**
* @param mixed $expression
*
* @return static
*/
public static function new($expression = null, string $returnType = ''): ArrowFunction
{
return new self($expression, $returnType);
return new static($expression, $returnType);
}

public function generate(): string
Expand All @@ -39,6 +47,8 @@ public function getExpression()

/**
* @param mixed $expression
*
* @return $this
*/
public function setExpression($expression): self
{
Expand All @@ -47,6 +57,11 @@ public function setExpression($expression): self
return $this;
}

/**
* @param mixed $value
*
* @return mixed
*/
protected function manageExprDependency($value)
{
if ($value instanceof DependencyAwareGenerator) {
Expand Down
9 changes: 8 additions & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Block extends AbstractGenerator implements BlockInterface
{
use ScopedContentTrait;

public final function __construct()
{
}

public function generate(): string
{
return <<<CODE
Expand All @@ -17,7 +21,10 @@ public function generate(): string
CODE;
}

public static function new()
/**
* @return static
*/
public static function new(): self
{
return new static();
}
Expand Down
9 changes: 6 additions & 3 deletions src/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ class Closure extends AbstractFunction
{
use ScopedContentTrait;

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

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

public function generate(): string
Expand Down
Loading

0 comments on commit 5beed9b

Please sign in to comment.