Skip to content

Commit

Permalink
SDK-4641 creation of the missed phpcs.xml (#150)
Browse files Browse the repository at this point in the history
SDK-4641 creation of the missed phpcs.xml
  • Loading branch information
pavelmaksimov25 authored Oct 27, 2023
2 parents 48f4eb9 + 89b29be commit b16d7d2
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 25 deletions.
16 changes: 16 additions & 0 deletions resources/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset name="SprykerLibrary">
<arg name="colors"/>
<arg name="tab-width" value="4"/>
<arg value="nps"/>

<file>src/</file>

<exclude-pattern>*/src/Generated/*</exclude-pattern>
<exclude-pattern>*/src/Orm/*/Base/</exclude-pattern>
<exclude-pattern>*/src/Orm/*/Map/</exclude-pattern>
<exclude-pattern>*/src/Orm/Propel/</exclude-pattern>

<rule ref="Spryker.Namespaces.UseStatement"/>
<rule ref="Spryker.Namespaces.UseWithAliasing"/>
</ruleset>
113 changes: 109 additions & 4 deletions src/Builder/FileNormalizer/CodeSnifferCompositeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,81 @@

namespace SprykerSdk\Integrator\Builder\FileNormalizer;

use SprykerSdk\Integrator\Composer\ComposerLockReaderInterface;
use SprykerSdk\Integrator\IntegratorConfig;
use Symfony\Component\Filesystem\Filesystem;

class CodeSnifferCompositeNormalizer implements FileNormalizerInterface
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Unable to execute code style fixer. Please manually execute it to adjust project code styles.';

/**
* @var string
*/
public const SPRYKER_CS_PACKAGE = 'spryker/code-sniffer';

/**
* @var string
*/
public const PHP_CS_FIXER_PATH = 'vendor/bin/phpcbf';

/**
* @var string
*/
public const PHP_CS_FIXER_CONFIG_PATH = 'phpcs.xml';

/**
* @var string
*/
protected const INTERNAL_PHP_CS_FIXER_CONFIG_PATH = 'resources/phpcs.xml';

/**
* @var array<\SprykerSdk\Integrator\Builder\FileNormalizer\FileNormalizerInterface>
*/
protected array $codeSniffNormalizers;

/**
* @param array<\SprykerSdk\Integrator\Builder\FileNormalizer\FileNormalizerInterface> $codeSniffNormalizers
* @var \SprykerSdk\Integrator\Composer\ComposerLockReaderInterface
*/
public function __construct(array $codeSniffNormalizers)
{
protected ComposerLockReaderInterface $composerLockReader;

/**
* @var \SprykerSdk\Integrator\IntegratorConfig
*/
protected IntegratorConfig $config;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected Filesystem $filesystem;

/**
* @param array $codeSniffNormalizers
* @param \SprykerSdk\Integrator\Composer\ComposerLockReaderInterface $composerLockReader
* @param \SprykerSdk\Integrator\IntegratorConfig $config
* @param \Symfony\Component\Filesystem\Filesystem $filesystem
*/
public function __construct(
array $codeSniffNormalizers,
ComposerLockReaderInterface $composerLockReader,
IntegratorConfig $config,
Filesystem $filesystem
) {
$this->codeSniffNormalizers = $codeSniffNormalizers;
$this->composerLockReader = $composerLockReader;
$this->config = $config;
$this->filesystem = $filesystem;
}

/**
* @return bool
*/
public function isApplicable(): bool
{
return count(array_filter($this->codeSniffNormalizers, static fn (FileNormalizerInterface $normalizer): bool => $normalizer->isApplicable())) > 0;
return true;
}

/**
Expand All @@ -51,6 +100,28 @@ public function getErrorMessage(): ?string
* @return void
*/
public function normalize(array $filePaths): void
{
if (!$this->isPhpCsConfigMissed()) {
$this->executeNormalizers($filePaths);

return;
}

$this->filesystem->copy(static::getInitialCsFixerConfig(), $this->getProjectCSFixerConfigPath());

try {
$this->executeNormalizers($filePaths);
} finally {
$this->filesystem->remove($this->getProjectCSFixerConfigPath());
}
}

/**
* @param array $filePaths
*
* @return void
*/
protected function executeNormalizers(array $filePaths): void
{
foreach ($this->codeSniffNormalizers as $codeSniffNormalizer) {
if (!$codeSniffNormalizer->isApplicable()) {
Expand All @@ -60,4 +131,38 @@ public function normalize(array $filePaths): void
$codeSniffNormalizer->normalize($filePaths);
}
}

/**
* @return bool
*/
protected function isPhpCsConfigMissed(): bool
{
return $this->composerLockReader->getPackageData(static::SPRYKER_CS_PACKAGE) !== null
&& $this->filesystem->exists($this->getProjectCSFixerPath())
&& !$this->filesystem->exists($this->getProjectCSFixerConfigPath());
}

/**
* @return string
*/
protected function getProjectCSFixerPath(): string
{
return $this->config->getProjectRootDirectory() . static::PHP_CS_FIXER_PATH;
}

/**
* @return string
*/
protected function getProjectCSFixerConfigPath(): string
{
return $this->config->getProjectRootDirectory() . static::PHP_CS_FIXER_CONFIG_PATH;
}

/**
* @return string
*/
public static function getInitialCsFixerConfig(): string
{
return dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . static::INTERNAL_PHP_CS_FIXER_CONFIG_PATH;
}
}
24 changes: 24 additions & 0 deletions src/Composer/ComposerLockReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ public function getModuleVersions(): array
return $packages;
}

/**
* @param string $packageName
*
* @return array<string>|null
*/
public function getPackageData(string $packageName): ?array
{
$composerLockData = $this->getProjectComposerLockData();

foreach (static::GROUP_PACKAGES as $packagesKey) {
if (!isset($composerLockData[$packagesKey])) {
continue;
}

foreach ($composerLockData[$packagesKey] as $packageData) {
if ($packageData['name'] === $packageName) {
return $packageData;
}
}
}

return null;
}

/**
* @param array<string, mixed> $packageData
*
Expand Down
7 changes: 7 additions & 0 deletions src/Composer/ComposerLockReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ interface ComposerLockReaderInterface
* @return array<string>
*/
public function getModuleVersions(): array;

/**
* @param string $packageName
*
* @return array|null
*/
public function getPackageData(string $packageName): ?array;
}
15 changes: 13 additions & 2 deletions src/IntegratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public function createWireTransferManifestStrategy(): ManifestStrategyInterface
{
return new WireTransferManifestStrategy(
$this->getConfig(),
new Filesystem(),
$this->createFilesystem(),
);
}

Expand All @@ -520,7 +520,7 @@ public function createWireSchemaManifestStrategy(): ManifestStrategyInterface
{
return new WireSchemaManifestStrategy(
$this->getConfig(),
new Filesystem(),
$this->createFilesystem(),
);
}

Expand Down Expand Up @@ -563,6 +563,9 @@ public function createCodeSnifferCompositeNormalizer(): FileNormalizerInterface
$this->createPhpCSFixerNormalizer(),
$this->createCodeSniffStyleFileNormalizer(),
],
$this->createComposerLockReader(),
$this->getConfig(),
$this->createFilesystem(),
);
}

Expand Down Expand Up @@ -1165,6 +1168,14 @@ protected function createManifestToModulesRatingRequestMapper(): ManifestToModul
return new ManifestToModulesRatingRequestMapper();
}

/**
* @return \Symfony\Component\Filesystem\Filesystem
*/
protected function createFilesystem(): Filesystem
{
return new Filesystem();
}

/**
* @return \SprykerSdk\Integrator\Builder\Finder\ClassConstantFinderInterface
*/
Expand Down
Loading

0 comments on commit b16d7d2

Please sign in to comment.