Skip to content

Commit

Permalink
incline string helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 12, 2023
1 parent 656a464 commit 4f6d6bb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 30 deletions.
11 changes: 0 additions & 11 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Yaml\Parser;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Php\TypeChecker;
use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
Expand All @@ -26,11 +22,4 @@
$services->set(Parser::class);
$services->set(BuilderFactory::class);
$services->set(ParentConnectingVisitor::class);

$services->set(TypeChecker::class);

$services->set(ParameterProvider::class)
->args([service('service_container')]);

$services->set(ClassLikeExistenceChecker::class);
};
19 changes: 15 additions & 4 deletions src/CaseConverter/AliasCaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker;
use Symplify\PhpConfigPrinter\Contract\CaseConverterInterface;
use Symplify\PhpConfigPrinter\Exception\ShouldNotHappenException;
use Symplify\PhpConfigPrinter\NodeFactory\ArgsNodeFactory;
Expand Down Expand Up @@ -40,7 +39,6 @@ public function __construct(
private readonly CommonNodeFactory $commonNodeFactory,
private readonly ArgsNodeFactory $argsNodeFactory,
private readonly ServiceOptionNodeFactory $serviceOptionNodeFactory,
private readonly ClassLikeExistenceChecker $classLikeExistenceChecker
) {
}

Expand All @@ -51,7 +49,7 @@ public function convertToMethodCallStmt(mixed $key, mixed $values): Stmt
}

$servicesVariable = new Variable(VariableName::SERVICES);
if ($this->classLikeExistenceChecker->doesClassLikeExist($key)) {
if ($this->doesClassLikeExist($key)) {
return $this->createFromClassLike($key, $values, $servicesVariable);
}

Expand Down Expand Up @@ -130,7 +128,7 @@ private function createFromClassLike(string $key, mixed $values, Variable $servi

private function createFromAlias(string $serviceName, string $key, Variable $servicesVariable): MethodCall
{
if ($this->classLikeExistenceChecker->doesClassLikeExist($serviceName)) {
if ($this->doesClassLikeExist($serviceName)) {
$classReference = $this->commonNodeFactory->createClassReference($serviceName);
$args = $this->argsNodeFactory->createFromValues([$key, $classReference]);
} else {
Expand All @@ -156,4 +154,17 @@ private function createFromArrayValues(array $values, string $key, Variable $ser
$methodCall = $this->serviceOptionNodeFactory->convertServiceOptionsToNodes($values, $methodCall);
return new Expression($methodCall);
}

private function doesClassLikeExist(string $class): bool
{
if (class_exists($class)) {
return true;
}

if (interface_exists($class)) {
return true;
}

return trait_exists($class);
}
}
7 changes: 2 additions & 5 deletions src/RoutingCaseConverter/ImportRoutingCaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Symplify\PackageBuilder\Strings\StringFormatConverter;
use Symplify\PhpConfigPrinter\Contract\RoutingCaseConverterInterface;
use Symplify\PhpConfigPrinter\Enum\RouteOption;
use Symplify\PhpConfigPrinter\NodeFactory\ArgsNodeFactory;
use Symplify\PhpConfigPrinter\Routing\ControllerSplitter;
use Symplify\PhpConfigPrinter\StringFormatConverter;
use Symplify\PhpConfigPrinter\ValueObject\VariableName;

final class ImportRoutingCaseConverter implements RoutingCaseConverterInterface
Expand Down Expand Up @@ -76,13 +76,10 @@ final class ImportRoutingCaseConverter implements RoutingCaseConverterInterface
*/
private const METHODS = 'methods';

private readonly StringFormatConverter $stringFormatConverter;

public function __construct(
private readonly ArgsNodeFactory $argsNodeFactory,
private readonly ControllerSplitter $controllerSplitter
) {
$this->stringFormatConverter = new StringFormatConverter();
}

/**
Expand Down Expand Up @@ -123,7 +120,7 @@ public function convertToMethodCall(string $key, mixed $values): Stmt
}

$args = $this->argsNodeFactory->createFromValues([$nestedValues]);
$name = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nestedKey);
$name = StringFormatConverter::underscoreAndHyphenToCamelCase($nestedKey);

$methodCall = new MethodCall($methodCall, $name, $args);
}
Expand Down
75 changes: 75 additions & 0 deletions src/StringFormatConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Symplify\PhpConfigPrinter;

use Nette\Utils\Strings;

/**
* @api
*/
final class StringFormatConverter
{
/**
* @var string
* @see https://regex101.com/r/rl1nvl/1
*/
private const BIG_LETTER_REGEX = '#([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]*)#';

public static function underscoreAndHyphenToCamelCase(string $value): string
{
$underscoreToHyphensValue = str_replace(['_', '-'], ' ', $value);
$uppercasedWords = ucwords($underscoreToHyphensValue);
$value = str_replace(' ', '', $uppercasedWords);

return lcfirst($value);
}

public static function camelCaseToUnderscore(string $input): string
{
return self::camelCaseToGlue($input, '_');
}

public static function camelCaseToDashed(string $input): string
{
return self::camelCaseToGlue($input, '-');
}

/**
* @param array<int|string, mixed> $items
* @return array<int|string, mixed>
*/
public static function camelCaseToUnderscoreInArrayKeys(array $items): array
{
foreach ($items as $key => $value) {
if (! is_string($key)) {
continue;
}

$newKey = self::camelCaseToUnderscore($key);
if ($key === $newKey) {
continue;
}

$items[$newKey] = $value;
unset($items[$key]);
}

return $items;
}

private static function camelCaseToGlue(string $input, string $glue): string
{
$matches = Strings::matchAll($input, self::BIG_LETTER_REGEX);

$parts = [];
foreach ($matches as $match) {
$parts[] = $match[0] === strtoupper((string) $match[0]) ? strtolower($match[0]) : lcfirst(
(string) $match[0]
);
}

return implode($glue, $parts);
}
}
13 changes: 3 additions & 10 deletions src/Yaml/CheckerServiceParametersShifter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Symplify\PhpConfigPrinter\Yaml;

use Nette\Utils\Strings;
use Symplify\PackageBuilder\Strings\StringFormatConverter;
use Symplify\PhpConfigPrinter\StringFormatConverter;

/**
* @api
Expand Down Expand Up @@ -71,13 +71,6 @@ final class CheckerServiceParametersShifter
'autowiring_types',
];

private readonly StringFormatConverter $stringFormatConverter;

public function __construct()
{
$this->stringFormatConverter = new StringFormatConverter();
}

/**
* @param mixed[] $configuration
* @return mixed[]
Expand Down Expand Up @@ -152,7 +145,7 @@ private function processFixer(array $services, string $checker, array $serviceDe
continue;
}

$serviceDefinition = $this->stringFormatConverter->camelCaseToUnderscoreInArrayKeys($serviceDefinition);
$serviceDefinition = StringFormatConverter::camelCaseToUnderscoreInArrayKeys($serviceDefinition);

$services[$checker]['calls'] = [['configure', [$serviceDefinition]]];
}
Expand All @@ -173,7 +166,7 @@ private function processSniff(array $services, string $checker, array $serviceDe
continue;
}

$key = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($key);
$key = StringFormatConverter::underscoreAndHyphenToCamelCase($key);

$services[$checker]['properties'][$key] = $this->escapeValue($value);
}
Expand Down

0 comments on commit 4f6d6bb

Please sign in to comment.