Skip to content

Commit

Permalink
#2276 - Add new property mixed in ClassMethod with own logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Sep 18, 2021
1 parent 59742ce commit 41d1ca6
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ class ClassMethod
*/
protected bool $void = false;

/**
* Whether the variable is mixed.
*
* Only for PHP >= 8.0
*
* @var bool
*/
protected bool $mixed = false;

/**
* Whether the method is public or not.
*
Expand Down Expand Up @@ -276,34 +285,42 @@ public function setReturnTypes(?array $returnType = null): void
$castTypes = [];

foreach ($returnType['list'] as $returnTypeItem) {
if (isset($returnTypeItem['cast'])) {
if (isset($returnTypeItem['cast']['collection'])) {
continue;
}
/**
* We continue the loop, because it only works for PHP >= 8.0.
*/
if (isset($returnTypeItem['data-type']) && $returnTypeItem['data-type'] === 'mixed') {
$this->mixed = true;
}

if (isset($returnTypeItem['collection']) && $returnTypeItem['collection']) {
$types['array'] = [
'type' => 'return-type-parameter',
'data-type' => 'array',
'mandatory' => 0,
'file' => $returnTypeItem['cast']['file'],
'line' => $returnTypeItem['cast']['line'],
'char' => $returnTypeItem['cast']['char'],
];
} else {
$castTypes[$returnTypeItem['cast']['value']] = $returnTypeItem['cast']['value'];
}
} else {
if (!isset($returnTypeItem['cast'])) {
$types[$returnTypeItem['data-type']] = $returnTypeItem;
continue;
}

if (isset($returnTypeItem['cast']['collection'])) {
continue;
}

if (isset($returnTypeItem['collection']) && $returnTypeItem['collection']) {
$types['array'] = [
'type' => 'return-type-parameter',
'data-type' => 'array',
'mandatory' => 0,
'file' => $returnTypeItem['cast']['file'],
'line' => $returnTypeItem['cast']['line'],
'char' => $returnTypeItem['cast']['char'],
];
} else {
$castTypes[$returnTypeItem['cast']['value']] = $returnTypeItem['cast']['value'];
}
}

if (count($castTypes)) {
if (count($castTypes) > 0) {
$types['object'] = [];
$this->returnClassTypes = $castTypes;
}

if (count($types)) {
if (count($types) > 0) {
$this->returnTypes = $types;
}
}
Expand Down Expand Up @@ -514,7 +531,7 @@ public function setupOptimized(CompilationContext $compilationContext): self
return $this;
}

public function getOptimizedMethod()
public function getOptimizedMethod(): ClassMethod
{
$optimizedName = $this->getName().'_zephir_internal_call';
$optimizedMethod = $this->classDefinition->getMethod($optimizedName, false);
Expand Down Expand Up @@ -633,7 +650,7 @@ public function areReturnTypesNullCompatible(): bool
*/
public function areReturnTypesIntCompatible(): bool
{
$types = ['int', 'uint', 'char', 'uchar', 'long', 'ulong'];
$types = ['int', 'uint', 'char', 'uchar', 'long', 'ulong', 'mixed'];

foreach ($this->returnTypes as $returnType => $definition) {
if (in_array($returnType, $types)) {
Expand Down Expand Up @@ -847,6 +864,16 @@ public function isVoid(): bool
return $this->void;
}

/**
* Checks if the methods return type is `mixed`.
*
* @return bool
*/
public function isMixed(): bool
{
return $this->mixed;
}

/**
* Checks if the method is inline.
*
Expand Down

0 comments on commit 41d1ca6

Please sign in to comment.