Skip to content

Commit

Permalink
Merge pull request #3 from symplify/tv-less-pb
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 12, 2023
2 parents 30366b8 + bd8c20d commit e4b9ba4
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 106 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.gitignore export-ignore
*.md export-ignore
/tests export-ignore
/easy-ci.php export-ignore
/ecs.php export-ignore
/phpstan.neon export-ignore
/rector.php export-ignore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

-
name: 'Check Active Classes'
run: vendor/bin/easy-ci check-active-class src --ansi
run: vendor/bin/class-leak check src --ansi --skip-type="\Symplify\PhpConfigPrinter\Contract\Converter\ServiceOptionsKeyYamlToPhpFactoryInterface" --skip-type="\Symplify\PhpConfigPrinter\Contract\RoutingCaseConverterInterface" --skip-type="\Symplify\PhpConfigPrinter\Contract\CaseConverterInterface"

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
"require": {
"php": ">=8.1",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.15.2",
"nikic/php-parser": "^4.16",
"symfony/yaml": "^6.2"
},
"require-dev": {
"myclabs/php-enum": "^1.8",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/extension-installer": "^1.2",
"phpunit/phpunit": "^10.0",
"rector/rector": "^0.15.18",
"symplify/easy-ci": "^11.1",
"symplify/easy-coding-standard": "^11.1",
"rector/rector": "^0.17",
"symplify/easy-ci": "^11.0",
"symplify/easy-coding-standard": "^12.0",
"symplify/easy-testing": "^11.1",
"symplify/phpstan-extensions": "^11.1",
"symplify/phpstan-rules": "11.2.3.72",
"symplify/symplify-kernel": "^11.1",
"tomasvotruba/unused-public": "^0.0.34"
"symplify/phpstan-extensions": "^11.2",
"symplify/phpstan-rules": "^12.0",
"symplify/symplify-kernel": "^11.0",
"tomasvotruba/class-leak": "0.1.1.72",
"tomasvotruba/unused-public": "^0.2.0"
},
"autoload": {
"psr-4": {
Expand Down
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);
};
56 changes: 0 additions & 56 deletions easy-ci.php

This file was deleted.

1 change: 0 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
$ecsConfig->paths([
__DIR__ . '/ecs.php',
__DIR__ . '/rector.php',
__DIR__ . '/easy-ci.php',
__DIR__ . '/src',
__DIR__ . '/tests',
]);
Expand Down
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ parameters:
- '*/tests/**/Fixture/*'
- '*/tests/**/data/*'

unused_public:
methods: true
properties: true
constants: true

ignoreErrors:
23 changes: 17 additions & 6 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 @@ -116,10 +114,10 @@ private function createAliasNode(string $key, string $fullClassName, mixed $serv

private function createFromClassLike(string $key, mixed $values, Variable $servicesVariable): Expression
{
$classReference = $this->commonNodeFactory->createClassReference($key);
$classConstFetch = $this->commonNodeFactory->createClassReference($key);

$argValues = [];
$argValues[] = $classReference;
$argValues[] = $classConstFetch;
$argValues[] = $values[MethodName::ALIAS] ?? $values;

$args = $this->argsNodeFactory->createFromValues($argValues, true);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function resolveExpressionWhenAtEnv(
$args = $expr->getArgs();

if (! isset($args[1]) || ! $args[1]->value instanceof Array_ || ! isset($args[1]->value->items[0])
|| ! $args[1]->value->items[0] instanceof ArrayItem || $args[1]->value->items[0]->key === null) {
|| ! $args[1]->value->items[0] instanceof ArrayItem || ! $args[1]->value->items[0]->key instanceof Expr) {
throw new ShouldNotHappenException();
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodeFactory/Service/SingleServicePhpNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function createCallMethodCall(mixed $call, MethodCall $methodCall): Meth
$args[] = new Arg($argumentsExpr);

$returnCloneExpr = $this->resolveCallReturnClone($call);
if ($returnCloneExpr !== null) {
if ($returnCloneExpr instanceof Expr) {
$args[] = new Arg($returnCloneExpr);
}

Expand Down
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 e4b9ba4

Please sign in to comment.