Skip to content

Commit

Permalink
#2239 - Refactor Stubs/* classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed May 9, 2021
1 parent d22cc10 commit a556787
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 54 deletions.
24 changes: 14 additions & 10 deletions Library/Stubs/DocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Stubs;

use function count;

/**
* @author Gusakov Nikita <dev@nkt.me>
*/
Expand All @@ -19,27 +23,27 @@ class DocBlock
/**
* @var string
*/
protected $indent;
protected string $indent;

/**
* @var string
*/
protected $description = '';
protected string $description = '';

/**
* @var array
*/
protected $lines = [];
protected array $lines = [];

/**
* @param string $source Raw doc-block
* @param string|null $source Raw doc-block
* @param string $indent Indent, 4 spaces by default
*/
public function __construct($source, $indent = ' ')
public function __construct(?string $source, string $indent = ' ')
{
$this->indent = $indent;
$lines = explode("\n", trim($source));
$count = \count($lines);
$lines = explode("\n", trim($source ?: ''));
$count = count($lines);

foreach ($lines as $i => $line) {
$line = preg_replace('#^([\s\t]+)?/?([*]+)([\s\t]+)?$#im', '', rtrim($line));
Expand All @@ -53,11 +57,11 @@ public function __construct($source, $indent = ' ')
$cleaned = str_replace('$$', '$', $cleaned);

if (0 === strpos($cleaned, '@')) {
$this->lines[] = $line = $cleaned;
$this->lines[] = $cleaned;
} else {
$line = preg_replace('#([\s\t]+)?[*]#', '', $line);
$line = preg_replace('#([\s\t]+)?[*]([\s\t]){1,2}#', '$1* ', ' * '.$line);
$line = preg_replace('#[*]([\s\t]){1,}$#', '*', $line);
$line = preg_replace('#[*]([\s\t])+$#', '*', $line);
$line = preg_replace('#\t#', $indent, $line);

$this->lines[] = array_pop($this->lines)."\n{$this->indent}".$line;
Expand Down Expand Up @@ -96,7 +100,7 @@ public function __construct($source, $indent = ' ')
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
$doc = '';
$indent = $this->indent;
Expand Down
54 changes: 28 additions & 26 deletions Library/Stubs/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Stubs;

use Zephir\AliasManager;
use Zephir\ClassConstant;
use Zephir\ClassDefinition;
use Zephir\ClassMethod;
use Zephir\ClassMethodParameters;
use Zephir\ClassProperty;
use Zephir\CompilerFile;
use Zephir\Exception;

use function array_key_exists;
use function count;
use function in_array;

/**
* Stubs Generator.
*/
Expand All @@ -29,7 +36,7 @@ class Generator
*
* @var array
*/
protected $ignoreModifiers = [
protected array $ignoreModifiers = [
'inline',
'internal',
'scoped',
Expand All @@ -39,7 +46,7 @@ class Generator
/**
* @var CompilerFile[]
*/
protected $files;
protected array $files;

/**
* @param CompilerFile[] $files
Expand Down Expand Up @@ -267,7 +274,7 @@ protected function buildMethod(ClassMethod $method, bool $isInterface, string $i

$parameters = [];

if ($methodParameters) {
if ($methodParameters instanceof ClassMethodParameters) {
foreach ($methodParameters->getParameters() as $parameter) {
$paramStr = '';
if (isset($parameter['cast'])) {
Expand All @@ -276,17 +283,18 @@ protected function buildMethod(ClassMethod $method, bool $isInterface, string $i
} else {
$cast = $parameter['cast']['value'];
}

$paramStr .= $cast.' ';
} elseif (isset($parameter['data-type']) && 'array' == $parameter['data-type']) {
} elseif (isset($parameter['data-type']) && 'array' === $parameter['data-type']) {
$paramStr .= 'array ';
} elseif (isset($parameter['data-type']) && version_compare(PHP_VERSION, '7.0.0', '>=')) {
if (\in_array($parameter['data-type'], ['bool', 'boolean'])) {
} elseif (isset($parameter['data-type'])) {
if (in_array($parameter['data-type'], ['bool', 'boolean'])) {
$paramStr .= 'bool ';
} elseif ('double' == $parameter['data-type']) {
$paramStr .= 'float ';
} elseif (\in_array($parameter['data-type'], ['int', 'uint', 'long', 'ulong', 'uchar'])) {
} elseif (in_array($parameter['data-type'], ['int', 'uint', 'long', 'ulong', 'uchar'])) {
$paramStr .= 'int ';
} elseif (\in_array($parameter['data-type'], ['char', 'string'])) {
} elseif (in_array($parameter['data-type'], ['char', 'string'])) {
$paramStr .= 'string ';
}
}
Expand All @@ -302,16 +310,10 @@ protected function buildMethod(ClassMethod $method, bool $isInterface, string $i
}

$return = '';
/**
* TODO: Add $method->isVoid() check after removing PHP 7.0 support.
*
* @see https://github.com/zephir-lang/zephir/issues/1977
* @see https://github.com/zephir-lang/zephir/pull/1978
*/
if (version_compare(PHP_VERSION, '7.0.0', '>=') && $method->hasReturnTypes()) {
if ($method->hasReturnTypes()) {
$supported = 0;

if (\array_key_exists('object', $method->getReturnTypes()) && 1 == \count($method->getReturnClassTypes())) {
if (array_key_exists('object', $method->getReturnTypes()) && 1 === count($method->getReturnClassTypes())) {
$return = key($method->getReturnClassTypes());
++$supported;
}
Expand All @@ -320,36 +322,36 @@ protected function buildMethod(ClassMethod $method, bool $isInterface, string $i
$return = 'int';
++$supported;
}

if ($method->areReturnTypesDoubleCompatible()) {
$return = 'float';
++$supported;
}

if ($method->areReturnTypesBoolCompatible()) {
$return = 'bool';
++$supported;
}

if ($method->areReturnTypesStringCompatible()) {
$return = 'string';
++$supported;
}
if (\array_key_exists('array', $method->getReturnTypes())) {

if (array_key_exists('array', $method->getReturnTypes())) {
$return = 'array';
++$supported;
}

if (!empty($return) && $method->areReturnTypesNullCompatible()) {
if (version_compare(PHP_VERSION, '7.1.0', '>=')) {
$return = '?'.$return;
} else {
$return = '';
}
$return = '?'.$return;
}

// PHP doesn't support multiple return types (yet?)
if ($supported > 1) {
$return = '';
}
} elseif (version_compare(PHP_VERSION, '7.1.0', '>=') && $method->isVoid()) {
} elseif ($method->isVoid()) {
$return = 'void';
}

Expand Down Expand Up @@ -391,7 +393,7 @@ protected function wrapPHPValue(array $parameter): string
break;

case 'empty-array':
$returnValue = 'array()';
$returnValue = '[]';
break;

case 'array':
Expand All @@ -413,7 +415,7 @@ protected function wrapPHPValue(array $parameter): string
]);
}

$returnValue = 'array('.implode(', ', $parameters).')';
$returnValue = '['.implode(', ', $parameters).']';
break;

case 'static-constant-access':
Expand All @@ -435,6 +437,6 @@ protected function wrapPHPValue(array $parameter): string
);
}

return $returnValue;
return (string)$returnValue;
}
}
59 changes: 41 additions & 18 deletions Library/Stubs/MethodDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Stubs;

use Zephir\AliasManager;
Expand All @@ -22,30 +24,51 @@
*/
class MethodDocBlock extends DocBlock
{
private $parameters = [];
private array $parameters = [];

/** Parameters which are described by User into docblock */
private $predefinedParams = [];
/**
* Parameters which are described by User into docblock
*
* @var array
*/
private array $predefinedParams = [];

private $return;
/**
* @var array
*/
private array $return = [];

private $shortcutName = '';
/**
* @var string|mixed
*/
private string $shortcutName;

private $deprecated = false;
/**
* @var bool
*/
private bool $deprecated;

/**
* @var AliasManager
*/
private $aliasManager;
private AliasManager $aliasManager;

/** @var ClassMethod */
private $classMethod;

/** @var Types */
private $types;
/**
* @var ClassMethod
*/
private ClassMethod $classMethod;

public function __construct(ClassMethod $method, AliasManager $aliasManager, $indent = ' ', Types $types = null)
{
/**
* @var Types
*/
private Types $types;

public function __construct(
ClassMethod $method,
AliasManager $aliasManager,
string $indent = ' ',
?Types $types = null
) {
parent::__construct($method->getDocBlock(), $indent);

$this->deprecated = $method->isDeprecated();
Expand Down Expand Up @@ -152,7 +175,7 @@ protected function parseDocBlockParam(string $line): array
return $matched;
}

protected function parseLines()
protected function parseLines(): void
{
$lines = [];

Expand Down Expand Up @@ -187,7 +210,7 @@ protected function parseLines()
$line = str_replace('@var', '@param', $line);
}

if ('var' == $docType && 'set' == $this->shortcutName) {
if ('var' === $docType && 'set' === $this->shortcutName) {
$docType = 'param';
$name = array_keys($this->parameters);
$name = $name[0];
Expand All @@ -212,7 +235,7 @@ protected function parseLines()
$this->lines = $lines;
}

private function appendReturnLine()
private function appendReturnLine(): void
{
if (!isset($this->predefinedParams['return'])) {
list($type, $description) = $this->return;
Expand Down Expand Up @@ -251,7 +274,7 @@ private function parseMethodParameters(ClassMethod $method)
}
}

private function appendParametersLines()
private function appendParametersLines(): void
{
foreach ($this->parameters as $name => $parameter) {
if (!isset($this->predefinedParams[trim($name, '$')])) {
Expand Down

0 comments on commit a556787

Please sign in to comment.