Skip to content

Commit

Permalink
#2283 - Mass code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Oct 2, 2021
1 parent 5c84fa6 commit 7932a48
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 308 deletions.
36 changes: 18 additions & 18 deletions Library/CacheManager.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;

use Zephir\Cache\ClassEntryCache;
Expand All @@ -18,43 +20,41 @@
use Zephir\Passes\CallGathererPass;

/**
* CacheManager.
*
* Creates and manages function, method and class entries caches
*/
class CacheManager
{
/**
* @var FunctionCache
* @var FunctionCache|null
*/
protected $functionCache;
protected ?FunctionCache $functionCache = null;

/**
* @var MethodCache
* @var MethodCache|null
*/
protected $methodCache;
protected ?MethodCache $methodCache = null;

/**
* @var StaticMethodCache
* @var StaticMethodCache|null
*/
protected $staticMethodCache;
protected ?StaticMethodCache $staticMethodCache = null;

/**
* @var ClassEntryCache
* @var ClassEntryCache|null
*/
protected $classEntryCache;
protected ?ClassEntryCache $classEntryCache = null;

/**
* @var CallGathererPass|null
*/
protected $gatherer;
protected ?CallGathererPass $gatherer = null;

/**
* Sets the CallGathererPass.
*
* @param CallGathererPass $gatherer
* @param CallGathererPass|null $gatherer
*/
public function setGatherer(CallGathererPass $gatherer = null)
public function setGatherer(?CallGathererPass $gatherer = null)
{
$this->gatherer = $gatherer;
}
Expand All @@ -64,7 +64,7 @@ public function setGatherer(CallGathererPass $gatherer = null)
*
* @return ClassEntryCache
*/
public function getClassEntryCache()
public function getClassEntryCache(): ClassEntryCache
{
if (!$this->classEntryCache) {
$this->classEntryCache = new ClassEntryCache();
Expand All @@ -78,7 +78,7 @@ public function getClassEntryCache()
*
* @return FunctionCache
*/
public function getFunctionCache()
public function getFunctionCache(): ?FunctionCache
{
if (!$this->functionCache) {
$this->functionCache = new FunctionCache($this->gatherer);
Expand All @@ -92,7 +92,7 @@ public function getFunctionCache()
*
* @return MethodCache
*/
public function getMethodCache()
public function getMethodCache(): ?MethodCache
{
if (!$this->methodCache) {
$this->methodCache = new MethodCache($this->gatherer);
Expand All @@ -106,10 +106,10 @@ public function getMethodCache()
*
* @return StaticMethodCache
*/
public function getStaticMethodCache()
public function getStaticMethodCache(): ?StaticMethodCache
{
if (!$this->staticMethodCache) {
$this->staticMethodCache = new StaticMethodCache($this->gatherer);
$this->staticMethodCache = new StaticMethodCache();
}

return $this->staticMethodCache;
Expand Down
2 changes: 0 additions & 2 deletions Library/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use Zephir\Expression\StaticConstantAccess;

/**
* ClassConstant.
*
* Represents a class constant
*/
class ClassConstant
Expand Down
65 changes: 24 additions & 41 deletions Library/CompiledExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
use Closure;

/**
* CompiledExpression.
*
* This represent a compiled expression, the object can be used to check
* if the expression type is able to be used in certain types of the application
* This represents a compiled expression, the object can be used to check
* if the expression type is able to used in certain types of the application.
*/
class CompiledExpression implements TypeAwareInterface
{
Expand Down Expand Up @@ -80,10 +78,10 @@ public function getBooleanCode(): string
{
if ($this->code && ('true' == $this->code || true === $this->code)) {
return '1';
} else {
if ('false' == $this->code || false === $this->code) {
return '0';
}
}

if ('false' === $this->code || false === $this->code) {
return '0';
}

return $this->code;
Expand All @@ -96,17 +94,7 @@ public function getBooleanCode(): string
*/
public function isIntCompatibleType(): bool
{
switch ($this->type) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
return true;
}

return false;
return in_array($this->type, ['int', 'uint', 'long', 'ulong', 'char', 'uchar'], true);
}

/**
Expand All @@ -116,17 +104,12 @@ public function isIntCompatibleType(): bool
*/
public function isCharCompatibleType(): bool
{
switch ($this->type) {
case 'char':
case 'uchar':
return true;
}

return false;
return in_array($this->type, ['char', 'uchar'], true);
}

/**
* Resolves an expression
*
* Some code cannot be directly pushed into the generated source
* because it's missing some bound parts, this method resolves the missing parts
* returning the generated code.
Expand All @@ -138,22 +121,22 @@ public function isCharCompatibleType(): bool
*/
public function resolve(?string $result, CompilationContext $compilationContext): string
{
if ($this->code instanceof Closure) {
$code = $this->code;
if (!$result) {
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->codePrinter->output($code($tempVariable->getName()));
$tempVariable->setIsInitialized(true, $compilationContext);

return $tempVariable->getName();
}

return $code($result);
if (!($this->code instanceof Closure)) {
return $this->code;
}

return $this->code;
$code = $this->code;
if (!$result) {
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->codePrinter->output($code($tempVariable->getName()));
$tempVariable->setIsInitialized(true, $compilationContext);

return $tempVariable->getName();
}

return $code($result);
}
}
Loading

0 comments on commit 7932a48

Please sign in to comment.