Skip to content

Commit

Permalink
SDK-2708 add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyspryker committed Oct 18, 2023
1 parent 0368f59 commit dcd51c8
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 79 deletions.
82 changes: 14 additions & 68 deletions src/Builder/Creator/AbstractMethodCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use PhpParser\BuilderFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface;
use SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface;
use SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface;
use SprykerSdk\Integrator\Transfer\ClassInformationTransfer;

class AbstractMethodCreator
Expand All @@ -24,46 +23,16 @@ class AbstractMethodCreator
protected const SIMPLE_VARIABLE_SEMICOLON_COUNT = 1;

/**
* @var string
* @var \SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface
*/
protected const STATIC_CONST_PREPOSITION = 'static';
protected PrefixedConstNameResolverInterface $prefixedConstNameResolver;

/**
* @var string
* @param \SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface $prefixedConstNameResolver
*/
protected const PARENT_CONST_PREPOSITION = 'parent';

/**
* @var string
*/
protected const PYZ_CONST_PREFIX = 'PYZ_';

/**
* @var array
*/
protected const CONST_PREPOSITIONS = [
self::STATIC_CONST_PREPOSITION,
self::PARENT_CONST_PREPOSITION,
];

/**
* @var \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface
*/
protected ClassConstantFinderInterface $classConstantFinder;

/**
* @var \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface
*/
protected ClassLoaderInterface $classLoader;

/**
* @param \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface $classConstantFinder
* @param \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface $classLoader
*/
public function __construct(ClassConstantFinderInterface $classConstantFinder, ClassLoaderInterface $classLoader)
public function __construct(PrefixedConstNameResolverInterface $prefixedConstNameResolver)
{
$this->classConstantFinder = $classConstantFinder;
$this->classLoader = $classLoader;
$this->prefixedConstNameResolver = $prefixedConstNameResolver;
}

/**
Expand All @@ -78,37 +47,14 @@ protected function createClassConstantExpression(
string $className,
string $constantName
): ClassConstFetch {
return (new BuilderFactory())->classConstFetch($className, $this->getClassConstantName($classInformationTransfer, $className, $constantName));
}

/**
* @param \SprykerSdk\Integrator\Transfer\ClassInformationTransfer $classInformationTransfer
* @param string $className
* @param string $constantName
*
* @return string
*/
protected function getClassConstantName(ClassInformationTransfer $classInformationTransfer, string $className, string $constantName): string
{
$targetClassName = $className;

if (in_array($className, static::CONST_PREPOSITIONS, true)) {
$targetClassName = $classInformationTransfer->getClassName();

if ($targetClassName === null) {
return $constantName;
}
}

if (!$this->classLoader->classExist($targetClassName)) {
return $constantName;
}

$targetClass = $this->classLoader->loadClass($targetClassName);

$prefixedConstantName = static::PYZ_CONST_PREFIX . $constantName;

return $this->classConstantFinder->findConstantByName($targetClass, $prefixedConstantName) ? $prefixedConstantName : $constantName;
return (new BuilderFactory())->classConstFetch(
$className,
$this->prefixedConstNameResolver->resolveClassConstantName(
$classInformationTransfer,
$className,
$constantName,
),
);
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Builder/Creator/MethodCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface;
use SprykerSdk\Integrator\Builder\Exception\LiteralValueParsingException;
use SprykerSdk\Integrator\Builder\Exception\NotFoundReturnExpressionException;
use SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface;
use SprykerSdk\Integrator\Builder\Finder\ClassNodeFinderInterface;
use SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface;
use SprykerSdk\Integrator\Builder\Visitor\AddMethodVisitor;
use SprykerSdk\Integrator\Transfer\ClassInformationTransfer;

Expand Down Expand Up @@ -68,19 +67,17 @@ class MethodCreator extends AbstractMethodCreator implements MethodCreatorInterf
* @param \SprykerSdk\Integrator\Builder\Creator\MethodDocBlockCreatorInterface $methodDocBlockCreator
* @param \SprykerSdk\Integrator\Builder\Creator\MethodReturnTypeCreatorInterface $methodReturnTypeCreator
* @param \PhpParser\ParserFactory $parserFactory
* @param \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface $classConstantFinder
* @param \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface $classLoader
* @param \SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface $prefixedConstNameResolver
*/
public function __construct(
ClassNodeFinderInterface $classNodeFinder,
MethodStatementsCreatorInterface $methodStatementsCreator,
MethodDocBlockCreatorInterface $methodDocBlockCreator,
MethodReturnTypeCreatorInterface $methodReturnTypeCreator,
ParserFactory $parserFactory,
ClassConstantFinderInterface $classConstantFinder,
ClassLoaderInterface $classLoader
PrefixedConstNameResolverInterface $prefixedConstNameResolver
) {
parent::__construct($classConstantFinder, $classLoader);
parent::__construct($prefixedConstNameResolver);

$this->classNodeFinder = $classNodeFinder;
$this->methodStatementsCreator = $methodStatementsCreator;
Expand Down
77 changes: 77 additions & 0 deletions src/Builder/Resolver/PrefixedConstNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

declare(strict_types=1);

namespace SprykerSdk\Integrator\Builder\Resolver;

use SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface;
use SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface;
use SprykerSdk\Integrator\Transfer\ClassInformationTransfer;

class PrefixedConstNameResolver implements PrefixedConstNameResolverInterface
{
/**
* @var array<string>
*/
protected const CONST_PREPOSITIONS = ['static', 'self', 'parent'];

/**
* @var string
*/
protected const PYZ_CONST_PREFIX = 'PYZ_';

/**
* @var \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface
*/
protected ClassConstantFinderInterface $classConstantFinder;

/**
* @var \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface
*/
protected ClassLoaderInterface $classLoader;

/**
* @param \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface $classConstantFinder
* @param \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface $classLoader
*/
public function __construct(ClassConstantFinderInterface $classConstantFinder, ClassLoaderInterface $classLoader)
{
$this->classConstantFinder = $classConstantFinder;
$this->classLoader = $classLoader;
}

/**
* @param \SprykerSdk\Integrator\Transfer\ClassInformationTransfer $classInformationTransfer
* @param string $className
* @param string $constantName
*
* @return string
*/
public function resolveClassConstantName(ClassInformationTransfer $classInformationTransfer, string $className, string $constantName): string
{
$targetClassName = $className;

if (in_array($className, static::CONST_PREPOSITIONS, true)) {
$targetClassName = $classInformationTransfer->getClassName();

if ($targetClassName === null) {
return $constantName;
}
}

if (!$this->classLoader->classExist($targetClassName)) {
return $constantName;
}

$targetClass = $this->classLoader->loadClass($targetClassName);

$prefixedConstantName = static::PYZ_CONST_PREFIX . $constantName;

return $this->classConstantFinder->findConstantByName($targetClass, $prefixedConstantName) ? $prefixedConstantName : $constantName;
}
}
24 changes: 24 additions & 0 deletions src/Builder/Resolver/PrefixedConstNameResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

declare(strict_types=1);

namespace SprykerSdk\Integrator\Builder\Resolver;

use SprykerSdk\Integrator\Transfer\ClassInformationTransfer;

interface PrefixedConstNameResolverInterface
{
/**
* @param \SprykerSdk\Integrator\Transfer\ClassInformationTransfer $classInformationTransfer
* @param string $className
* @param string $constantName
*
* @return string
*/
public function resolveClassConstantName(ClassInformationTransfer $classInformationTransfer, string $className, string $constantName): string;
}
16 changes: 12 additions & 4 deletions src/IntegratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
use SprykerSdk\Integrator\Builder\Printer\ClassDiffPrinter;
use SprykerSdk\Integrator\Builder\Printer\ClassDiffPrinterInterface;
use SprykerSdk\Integrator\Builder\Printer\ClassPrinter;
use SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolver;
use SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface;
use SprykerSdk\Integrator\Builder\Visitor\PluginPositionResolver\PluginPositionResolver;
use SprykerSdk\Integrator\Builder\Visitor\PluginPositionResolver\PluginPositionResolverInterface;
use SprykerSdk\Integrator\Communication\ReleaseApp\ModuleRatingFetcher;
Expand Down Expand Up @@ -620,6 +622,14 @@ public function createClassResolver(): ClassResolverInterface
return new ClassResolver($this->createClassLoader(), $this->createClassGenerator());
}

/**
* @return \SprykerSdk\Integrator\Builder\Resolver\PrefixedConstNameResolverInterface
*/
protected function createPrefixedConstNameResolver(): PrefixedConstNameResolverInterface
{
return new PrefixedConstNameResolver($this->createClassConstantFinder(), $this->createClassLoader());
}

/**
* @return \SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface
*/
Expand Down Expand Up @@ -696,8 +706,7 @@ public function createMethodCreator(): MethodCreatorInterface
$this->createMethodDocBlockCreator(),
$this->createMethodReturnTypeCreator(),
$this->createParserFactory(),
$this->createClassConstantFinder(),
$this->createClassLoader(),
$this->createPrefixedConstNameResolver(),
);
}

Expand All @@ -723,8 +732,7 @@ public function createMethodReturnTypeCreator(): MethodReturnTypeCreatorInterfac
public function createMethodStatementsCreator(): MethodStatementsCreatorInterface
{
return new MethodStatementsCreator(
$this->createClassConstantFinder(),
$this->createClassLoader(),
$this->createPrefixedConstNameResolver(),
);
}

Expand Down
Loading

0 comments on commit dcd51c8

Please sign in to comment.