From ba3d28dc45fc6b53093e09e406a8d733eb85ac19 Mon Sep 17 00:00:00 2001 From: Vladislav Strelchenko <85889893+VladislavStrelchenko@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:39:47 +0200 Subject: [PATCH] SDK-4713: Extract utils (#155) --- composer.json | 2 +- src/Builder/ClassGenerator/ClassGenerator.php | 4 +- .../CodeSniffStyleFileNormalizer.php | 14 +-- .../PhpCSFixerFileNormalizer.php | 18 ++-- .../UtilText/Filter/CamelCaseToDash.php | 32 ------- src/Common/UtilText/TextCaseHelper.php | 60 ------------- src/Composer/ComposerLockReader.php | 4 +- src/IntegratorFactory.php | 14 +-- .../CopyModuleFileManifestStrategy.php | 6 +- .../CodeSniffStyleFileNormalizerTest.php | 14 +-- .../PhpCSFixerFileNormalizerTest.php | 14 +-- .../Common/UtilText/TextCaseHelperTest.php | 87 ------------------- 12 files changed, 45 insertions(+), 224 deletions(-) delete mode 100644 src/Common/UtilText/Filter/CamelCaseToDash.php delete mode 100644 src/Common/UtilText/TextCaseHelper.php delete mode 100644 tests/SprykerSdkTest/Integrator/Common/UtilText/TextCaseHelperTest.php diff --git a/composer.json b/composer.json index 9f10a5ef..f4203db9 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "composer/composer": "^2.1.0", "czproject/git-php": "^4.1", "guzzlehttp/guzzle": "^7.4", - "laminas/laminas-filter": "^2.11.0", + "spryker-sdk/utils": "dev-master", "nikic/php-parser": "^4.3.0", "sebastian/diff": "^4.0.0", "symfony/console": "^5.3.0 || ^6.0", diff --git a/src/Builder/ClassGenerator/ClassGenerator.php b/src/Builder/ClassGenerator/ClassGenerator.php index 3f67718c..f725fb9e 100644 --- a/src/Builder/ClassGenerator/ClassGenerator.php +++ b/src/Builder/ClassGenerator/ClassGenerator.php @@ -14,10 +14,10 @@ use PhpParser\BuilderFactory; use PhpParser\Node\Name; use SprykerSdk\Integrator\Builder\ClassLoader\ClassLoaderInterface; -use SprykerSdk\Integrator\Common\UtilText\TextCaseHelper; use SprykerSdk\Integrator\Helper\ClassHelperInterface; use SprykerSdk\Integrator\IntegratorConfig; use SprykerSdk\Integrator\Transfer\ClassInformationTransfer; +use SprykerSdk\Utils\Infrastructure\Helper\StrHelper; class ClassGenerator implements ClassGeneratorInterface { @@ -163,7 +163,7 @@ protected function resolveModuleDir(string $organisation, string $module): strin { if (in_array($organisation, $this->config->getCoreNonSplitOrganisations())) { return $this->config->getVendorDirectory() - . sprintf($this->config->getNonSplitRepositoryPathPattern(), TextCaseHelper::camelCaseToDash($organisation, false)) + . sprintf($this->config->getNonSplitRepositoryPathPattern(), StrHelper::camelCaseToDash($organisation, false)) . $module . DIRECTORY_SEPARATOR; } diff --git a/src/Builder/FileNormalizer/CodeSniffStyleFileNormalizer.php b/src/Builder/FileNormalizer/CodeSniffStyleFileNormalizer.php index 09d517da..2ae0db8b 100644 --- a/src/Builder/FileNormalizer/CodeSniffStyleFileNormalizer.php +++ b/src/Builder/FileNormalizer/CodeSniffStyleFileNormalizer.php @@ -10,8 +10,8 @@ namespace SprykerSdk\Integrator\Builder\FileNormalizer; use RuntimeException; -use SprykerSdk\Integrator\Executor\ProcessExecutorInterface; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface; class CodeSniffStyleFileNormalizer implements FileNormalizerInterface { @@ -31,18 +31,18 @@ class CodeSniffStyleFileNormalizer implements FileNormalizerInterface protected $config; /** - * @var \SprykerSdk\Integrator\Executor\ProcessExecutorInterface + * @var \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface */ - protected ProcessExecutorInterface $processExecutor; + protected ProcessRunnerServiceInterface $processRunner; /** * @param \SprykerSdk\Integrator\IntegratorConfig $config - * @param \SprykerSdk\Integrator\Executor\ProcessExecutorInterface $processExecutor + * @param \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface $processRunner */ - public function __construct(IntegratorConfig $config, ProcessExecutorInterface $processExecutor) + public function __construct(IntegratorConfig $config, ProcessRunnerServiceInterface $processRunner) { $this->config = $config; - $this->processExecutor = $processExecutor; + $this->processRunner = $processRunner; } /** @@ -72,7 +72,7 @@ public function normalize(array $filePaths): void { $projectConsolePath = $this->getProjectConsolePath(); foreach ($this->getProjectRelativeFilePaths($filePaths) as $filePath) { - $process = $this->processExecutor->execute([$projectConsolePath, static::PHP_CS_FIX_COMMAND, $filePath]); + $process = $this->processRunner->run([$projectConsolePath, static::PHP_CS_FIX_COMMAND, $filePath]); if ($process->getExitCode() > 0 && $process->getErrorOutput() !== '') { throw new RuntimeException($process->getErrorOutput()); diff --git a/src/Builder/FileNormalizer/PhpCSFixerFileNormalizer.php b/src/Builder/FileNormalizer/PhpCSFixerFileNormalizer.php index 04449295..f7e450d8 100644 --- a/src/Builder/FileNormalizer/PhpCSFixerFileNormalizer.php +++ b/src/Builder/FileNormalizer/PhpCSFixerFileNormalizer.php @@ -10,8 +10,8 @@ namespace SprykerSdk\Integrator\Builder\FileNormalizer; use RuntimeException; -use SprykerSdk\Integrator\Executor\ProcessExecutorInterface; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface; class PhpCSFixerFileNormalizer implements FileNormalizerInterface { @@ -26,18 +26,18 @@ class PhpCSFixerFileNormalizer implements FileNormalizerInterface protected $config; /** - * @var \SprykerSdk\Integrator\Executor\ProcessExecutorInterface + * @var \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface */ - protected ProcessExecutorInterface $processExecutor; + protected ProcessRunnerServiceInterface $processRunner; /** * @param \SprykerSdk\Integrator\IntegratorConfig $config - * @param \SprykerSdk\Integrator\Executor\ProcessExecutorInterface $processExecutor + * @param \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface $processRunner */ - public function __construct(IntegratorConfig $config, ProcessExecutorInterface $processExecutor) + public function __construct(IntegratorConfig $config, ProcessRunnerServiceInterface $processRunner) { $this->config = $config; - $this->processExecutor = $processExecutor; + $this->processRunner = $processRunner; } /** @@ -66,13 +66,13 @@ public function getErrorMessage(): ?string public function normalize(array $filePaths): void { $command = [$this->getCSFixPath(), ...$this->getAbsoluteFilePaths($filePaths)]; - $process = $this->processExecutor->execute($command); + $process = $this->processRunner->run($command); // TODO remove when phpcbf will be able to fix all issues in file during the one iteration if (defined('TEST_INTEGRATOR_MODE') && TEST_INTEGRATOR_MODE === 'true' && $process->getExitCode() !== 0) { - $process = $this->processExecutor->execute($command); + $process = $this->processRunner->run($command); if ($process->getExitCode() !== 0) { - $process = $this->processExecutor->execute($command); + $process = $this->processRunner->run($command); } } diff --git a/src/Common/UtilText/Filter/CamelCaseToDash.php b/src/Common/UtilText/Filter/CamelCaseToDash.php deleted file mode 100644 index 48c6f859..00000000 --- a/src/Common/UtilText/Filter/CamelCaseToDash.php +++ /dev/null @@ -1,32 +0,0 @@ -getSeparator(), '$') . '$2', $string); - - return is_array($value) ? (string)array_shift($value) : (string)$value; - } -} diff --git a/src/Common/UtilText/TextCaseHelper.php b/src/Common/UtilText/TextCaseHelper.php deleted file mode 100644 index 73805da6..00000000 --- a/src/Common/UtilText/TextCaseHelper.php +++ /dev/null @@ -1,60 +0,0 @@ -attach(new CamelCaseToDash()); - } else { - $filterChain->attach(new CamelCaseToDashWithoutAbbreviation()); - } - - $filterChain->attach(new StringToLower()); - - return $filterChain->filter($value); - } - - /** - * @param string $value - * @param bool $upperCaseFirst - * - * @return string - */ - public static function dashToCamelCase(string $value, bool $upperCaseFirst = true): string - { - $filterChain = new FilterChain(); - $filterChain->attach(new DashToCamelCase()); - - if ($upperCaseFirst) { - return ucfirst($filterChain->filter($value)); - } - - // Set first character in original case - - return mb_substr($value, 0, 1) . mb_substr($filterChain->filter($value), 1); - } -} diff --git a/src/Composer/ComposerLockReader.php b/src/Composer/ComposerLockReader.php index ae13abf4..0e1965e2 100644 --- a/src/Composer/ComposerLockReader.php +++ b/src/Composer/ComposerLockReader.php @@ -9,8 +9,8 @@ namespace SprykerSdk\Integrator\Composer; -use SprykerSdk\Integrator\Common\UtilText\TextCaseHelper; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Helper\StrHelper; class ComposerLockReader implements ComposerLockReaderInterface { @@ -110,7 +110,7 @@ public function getPackageData(string $packageName): ?array protected function getPackageVersion(array $packageData): array { [$org, $module] = explode('/', $packageData['name']); - $packageName = sprintf('%s.%s', TextCaseHelper::dashToCamelCase($org), TextCaseHelper::dashToCamelCase($module)); + $packageName = sprintf('%s.%s', StrHelper::dashToCamelCase($org), StrHelper::dashToCamelCase($module)); if (strpos($packageData['version'], 'dev-') !== false) { $versionFromExtra = $packageData['extra']['branch-alias']['dev-master'] ?? false; diff --git a/src/IntegratorFactory.php b/src/IntegratorFactory.php index ee79e34e..3156d102 100644 --- a/src/IntegratorFactory.php +++ b/src/IntegratorFactory.php @@ -119,8 +119,6 @@ use SprykerSdk\Integrator\Executor\ManifestExecutorInterface; use SprykerSdk\Integrator\Executor\Module\ModuleManifestExecutor; use SprykerSdk\Integrator\Executor\Module\ModuleManifestExecutorInterface; -use SprykerSdk\Integrator\Executor\ProcessExecutor; -use SprykerSdk\Integrator\Executor\ProcessExecutorInterface; use SprykerSdk\Integrator\Executor\ReleaseGroup\DiffGenerator; use SprykerSdk\Integrator\FileStorage\BucketFileStorage; use SprykerSdk\Integrator\FileStorage\BucketFileStorageInterface; @@ -156,6 +154,8 @@ use SprykerSdk\Integrator\ManifestStrategy\WireTransferManifestStrategy; use SprykerSdk\Integrator\ManifestStrategy\WireWidgetManifestStrategy; use SprykerSdk\Integrator\VersionControlSystem\GitRepository; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerService; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface; use Symfony\Component\Filesystem\Filesystem; class IntegratorFactory @@ -574,7 +574,7 @@ public function createCodeSnifferCompositeNormalizer(): FileNormalizerInterface */ public function createCodeSniffStyleFileNormalizer(): FileNormalizerInterface { - return new CodeSniffStyleFileNormalizer($this->getConfig(), $this->createProcessExecutor()); + return new CodeSniffStyleFileNormalizer($this->getConfig(), $this->createProcessRunnerService()); } /** @@ -582,15 +582,15 @@ public function createCodeSniffStyleFileNormalizer(): FileNormalizerInterface */ public function createPhpCSFixerNormalizer(): FileNormalizerInterface { - return new PhpCSFixerFileNormalizer($this->getConfig(), $this->createProcessExecutor()); + return new PhpCSFixerFileNormalizer($this->getConfig(), $this->createProcessRunnerService()); } /** - * @return \SprykerSdk\Integrator\Executor\ProcessExecutorInterface + * @return \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface */ - public function createProcessExecutor(): ProcessExecutorInterface + public function createProcessRunnerService(): ProcessRunnerServiceInterface { - return new ProcessExecutor(); + return new ProcessRunnerService(); } /** diff --git a/src/ManifestStrategy/CopyModuleFileManifestStrategy.php b/src/ManifestStrategy/CopyModuleFileManifestStrategy.php index 366167a6..f53b4361 100644 --- a/src/ManifestStrategy/CopyModuleFileManifestStrategy.php +++ b/src/ManifestStrategy/CopyModuleFileManifestStrategy.php @@ -9,10 +9,10 @@ namespace SprykerSdk\Integrator\ManifestStrategy; -use SprykerSdk\Integrator\Common\UtilText\TextCaseHelper; use SprykerSdk\Integrator\Dependency\Console\InputOutputInterface; use SprykerSdk\Integrator\Exception\ManifestApplyingException; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Helper\StrHelper; class CopyModuleFileManifestStrategy extends AbstractManifestStrategy { @@ -81,9 +81,9 @@ protected function getSourcePath(string $source, string $moduleName): string [$organisation, $moduleName] = explode('.', $moduleName); return $this->config->getVendorDirectory() - . mb_strtolower(TextCaseHelper::camelCaseToDash($organisation)) + . mb_strtolower(StrHelper::camelCaseToDash($organisation)) . DIRECTORY_SEPARATOR - . mb_strtolower(TextCaseHelper::camelCaseToDash($moduleName)) + . mb_strtolower(StrHelper::camelCaseToDash($moduleName)) . DIRECTORY_SEPARATOR . $source; } diff --git a/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/CodeSniffStyleFileNormalizerTest.php b/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/CodeSniffStyleFileNormalizerTest.php index a07ce3a3..d218925b 100644 --- a/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/CodeSniffStyleFileNormalizerTest.php +++ b/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/CodeSniffStyleFileNormalizerTest.php @@ -13,9 +13,9 @@ use RuntimeException; use SprykerSdk\Integrator\Builder\FileNormalizer\CodeSniffStyleFileNormalizer; use SprykerSdk\Integrator\Builder\FileStorage\FileStorage; -use SprykerSdk\Integrator\Executor\ProcessExecutor; -use SprykerSdk\Integrator\Executor\ProcessExecutorInterface; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerService; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface; use Symfony\Component\Process\Process; class CodeSniffStyleFileNormalizerTest extends TestCase @@ -27,7 +27,7 @@ public function testExecuteSuccess(): void { // Arrange $processExecutorMock = $this->createProcessExecutorMock(0, ''); - $processExecutorMock->expects($this->once())->method('execute')->with( + $processExecutorMock->expects($this->once())->method('run')->with( $this->callback(function ($command) { return $command[1] === 'code:sniff:style -f'; }), @@ -85,13 +85,13 @@ public function testGetErrorMessageShouldReturnErrorMessage(): void * @param int $exitCode * @param string $errorOutput * - * @return \SprykerSdk\Integrator\Executor\ProcessExecutorInterface + * @return \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface */ - protected function createProcessExecutorMock(int $exitCode, string $errorOutput = ''): ProcessExecutorInterface + protected function createProcessExecutorMock(int $exitCode, string $errorOutput = ''): ProcessRunnerServiceInterface { $processMock = $this->createProcessMock($exitCode, $errorOutput); - $processExecutorMock = $this->createMock(ProcessExecutor::class); - $processExecutorMock->method('execute')->willReturn($processMock); + $processExecutorMock = $this->createMock(ProcessRunnerService::class); + $processExecutorMock->method('run')->willReturn($processMock); return $processExecutorMock; } diff --git a/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/PhpCSFixerFileNormalizerTest.php b/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/PhpCSFixerFileNormalizerTest.php index f4f8a9f2..5d358087 100644 --- a/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/PhpCSFixerFileNormalizerTest.php +++ b/tests/SprykerSdkTest/Integrator/Builder/FileNormalizer/PhpCSFixerFileNormalizerTest.php @@ -13,9 +13,9 @@ use RuntimeException; use SprykerSdk\Integrator\Builder\FileNormalizer\PhpCSFixerFileNormalizer; use SprykerSdk\Integrator\Builder\FileStorage\FileStorage; -use SprykerSdk\Integrator\Executor\ProcessExecutor; -use SprykerSdk\Integrator\Executor\ProcessExecutorInterface; use SprykerSdk\Integrator\IntegratorConfig; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerService; +use SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface; use Symfony\Component\Process\Process; class PhpCSFixerFileNormalizerTest extends TestCase @@ -27,7 +27,7 @@ public function testExecuteSuccess(): void { // Arrange $processExecutorMock = $this->createProcessExecutorMock(0, ''); - $processExecutorMock->expects($this->atLeastOnce())->method('execute')->with( + $processExecutorMock->expects($this->atLeastOnce())->method('run')->with( $this->callback(function ($command) { return strpos($command[0], 'vendor/bin/phpcbf') !== false; }), @@ -85,13 +85,13 @@ public function testGetErrorMessageShouldReturnErrorMessage(): void * @param int $exitCode * @param string $errorOutput * - * @return \SprykerSdk\Integrator\Executor\ProcessExecutorInterface + * @return \SprykerSdk\Utils\Infrastructure\Service\ProcessRunnerServiceInterface */ - protected function createProcessExecutorMock(int $exitCode, string $errorOutput = ''): ProcessExecutorInterface + protected function createProcessExecutorMock(int $exitCode, string $errorOutput = ''): ProcessRunnerServiceInterface { $processMock = $this->createProcessMock($exitCode, $errorOutput); - $processExecutorMock = $this->createMock(ProcessExecutor::class); - $processExecutorMock->method('execute')->willReturn($processMock); + $processExecutorMock = $this->createMock(ProcessRunnerService::class); + $processExecutorMock->method('run')->willReturn($processMock); return $processExecutorMock; } diff --git a/tests/SprykerSdkTest/Integrator/Common/UtilText/TextCaseHelperTest.php b/tests/SprykerSdkTest/Integrator/Common/UtilText/TextCaseHelperTest.php deleted file mode 100644 index 6dfab75d..00000000 --- a/tests/SprykerSdkTest/Integrator/Common/UtilText/TextCaseHelperTest.php +++ /dev/null @@ -1,87 +0,0 @@ -assertSame($expResult, TextCaseHelper::camelCaseToDash($value, $separateAbbreviation)); - } - - /** - * @return \Generator - */ - public function dashToCamelCaseDataProvider(): Generator - { - $data = [ - ['TestFooBar', 'test-foo-bar', true], - ['testFooBar', 'test-foo-bar', false], - ['TestFooBar', 'Test-foo-bar', false], - ['TestFooBarAbcTest', 'test-foo-bar-abc-test', true], - ['testFooBarAbctest', 'test-foo-bar-abctest', false], - ['TestFooBarAbc123Test', 'test-foo-bar-abc123-test', true], - ['TestFooBarAbc123Test', 'test-foo-bar-abc-123-test', true], - ['TestFooBarAbc123test', 'test-foo-bar-abc-123test', true], - ]; - - foreach ($data as $set) { - yield $set; - } - } - - /** - * @dataProvider dashToCamelCaseDataProvider - * - * @param string $expResult - * @param string $value - * @param bool $upperCaseFirst - * - * @return void - */ - public function testDashToCamelCase(string $expResult, string $value, bool $upperCaseFirst): void - { - $this->assertSame($expResult, TextCaseHelper::dashToCamelCase($value, $upperCaseFirst)); - } -}