Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/header-stamp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ foreach ($autoloadFiles as $autoloadFile) {
use PrestaShop\HeaderStamp\Command\UpdateLicensesCommand;
use Symfony\Component\Console\Application;

$application = new Application('header-stamp', '3.0.0');
$application = new Application('header-stamp', '3.1.0');
$command = new UpdateLicensesCommand();

$application->add($command);
Expand Down
32 changes: 24 additions & 8 deletions src/Command/UpdateLicensesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class UpdateLicensesCommand extends Command
'targetDirectory' => '',
'runAsDry' => false,
'displayReport' => false,
'discriminationString' => 'NOTICE OF LICENSE',
'discriminationString' => [
'PrestaShop SA and Contributors',
'NOTICE OF LICENSE',
],
];

const DEFAULT_CONFIG_FILE = '.header-stamp-config.yml';
Expand Down Expand Up @@ -137,7 +140,7 @@ class UpdateLicensesCommand extends Command
private $reporter;

/**
* @var string
* @var string[]
*/
private $discriminationString;

Expand Down Expand Up @@ -196,7 +199,7 @@ protected function configure(): void
'header-discrimination-string',
null,
InputOption::VALUE_OPTIONAL,
'Fix existing licenses only if they contain that string'
'Fix existing licenses only if they contain that string (multiple values separated by comma are possible)'
)
->addOption(
'config',
Expand Down Expand Up @@ -238,6 +241,9 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
if (is_string($mergedConfig['notNamePatterns'])) {
$mergedConfig['notNamePatterns'] = explode(',', $mergedConfig['notNamePatterns']);
}
if (is_string($mergedConfig['discriminationString'])) {
$mergedConfig['discriminationString'] = explode(',', $mergedConfig['discriminationString']);
}
// Adapt boolean parameters
$mergedConfig['runAsDry'] = filter_var($mergedConfig['runAsDry'], FILTER_VALIDATE_BOOLEAN);
$mergedConfig['displayReport'] = filter_var($mergedConfig['displayReport'], FILTER_VALIDATE_BOOLEAN);
Expand Down Expand Up @@ -268,7 +274,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
/**
* Return the config only based on explicitly specified parameters in the CLI command.
*
* @return array{extensions?: string[], excludedFiles?: string[], notNamePatterns?: string[], license?: string, targetDirectory?: string, runAsDry?: bool, displayReport?: bool, discriminationString?: string}
* @return array{extensions?: string[], excludedFiles?: string[], notNamePatterns?: string[], license?: string, targetDirectory?: string, runAsDry?: bool, displayReport?: bool, discriminationString?: string[]}
*/
protected function getTokenConfig(InputInterface $input): array
{
Expand All @@ -290,7 +296,7 @@ protected function getTokenConfig(InputInterface $input): array
*
* Ex: notNamePatterns will be preferred over not-name
*
* @return array{extensions?: string[], excludedFiles?: string[], notNamePatterns?: string[], license?: string, targetDirectory?: string, runAsDry?: bool, displayReport?: bool, discriminationString?: string}
* @return array{extensions?: string, excludedFiles?: string, notNamePatterns?: string, license?: string, targetDirectory?: string, runAsDry?: bool, displayReport?: bool, discriminationString?: string}
*/
protected function getConfigFromFile(InputInterface $input): array
{
Expand Down Expand Up @@ -431,7 +437,7 @@ private function addLicenseToFile(SplFileInfo $file, string $regex): void
if (count($matches)) {
// Found - Replace it if prestashop one
foreach ($matches as $match) {
if (stripos($match, $this->discriminationString) !== false) {
if ($this->isLicenseComment($match)) {
$foundLicenseComment = true;
$content = str_replace($match, $text, $content);
}
Expand Down Expand Up @@ -459,8 +465,7 @@ private function addLicenseToNode(Stmt $node, SplFileInfo $file): void

$comments = $node->getAttribute('comments');
foreach ($comments as $comment) {
if ($comment instanceof \PhpParser\Comment
&& strpos($comment->getText(), $this->discriminationString) !== false) {
if ($comment instanceof \PhpParser\Comment && $this->isLicenseComment($comment->getText())) {
$newContent = str_replace($comment->getText(), $this->licenseHeader->getContentByType('php'), $file->getContents());

if (!$this->runAsDry) {
Expand All @@ -480,6 +485,17 @@ private function addLicenseToNode(Stmt $node, SplFileInfo $file): void
$this->prependInPHPFile($file);
}

private function isLicenseComment(string $commentContent): bool
{
foreach ($this->discriminationString as $discriminationString) {
if (strpos($commentContent, $discriminationString) !== false) {
return true;
}
}

return false;
}

private function prependInPHPFile(SplFileInfo $file): void
{
$needle = '<?php';
Expand Down
21 changes: 16 additions & 5 deletions tests/Integration/Command/UpdateLicencesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected static function cleanWorkspace(): void
/**
* @dataProvider getFoldersToTest
*/
public function testCommandModifications(string $folderToTest, bool $isFolderValid, array $invalidFiles = []): void
public function testCommandModifications(string $folderToTest, bool $isFolderValid, array $invalidFiles = [], string $discriminationParam = ''): void
{
// Prepare module workspace
$moduleSource = __DIR__ . '/../../Resources/module-samples/' . $folderToTest;
Expand All @@ -83,9 +83,10 @@ public function testCommandModifications(string $folderToTest, bool $isFolderVal
'--not-name' => '*.min.js',
'--exclude' => 'ignoredFolder',
];
if ('existing-headers-discrimination' === $folderToTest) {
$commandParameters['--header-discrimination-string'] = 'friendsofpresta';
if (!empty($discriminationParam)) {
$commandParameters['--header-discrimination-string'] = $discriminationParam;
}

$commandTester->execute($commandParameters);

// Compare folders
Expand Down Expand Up @@ -135,9 +136,10 @@ public function testCommandDryRun(string $folderToTest, bool $isFolderValid, arr
'--exclude' => 'ignoredFolder',
'--dry-run' => true,
];
if ('existing-headers-discrimination' === $folderToTest) {
$commandParameters['--header-discrimination-string'] = 'friendsofpresta';
if (!empty($discriminationParam)) {
$commandParameters['--header-discrimination-string'] = $discriminationParam;
}

$commandResult = $commandTester->execute($commandParameters);
$this->assertEquals($isFolderValid ? 0 : 1, $commandResult);

Expand Down Expand Up @@ -195,16 +197,24 @@ public static function getFoldersToTest(): iterable
'FakeClassWithWrongHeader.php',
'composer.json',
],
// Use two discriminators (to check the implode works as expected)
'PrestaShop SA and Contributors,NOTICE OF LICENSE',
];

yield 'valid module gsitemap' => [
'gsitemap',
true,
[],
// Use only one discriminator
'NOTICE OF LICENSE',
];

yield 'valid module dashproducts' => [
'dashproducts',
true,
[],
// Use only one discriminator, but the other one (both are supposed to work with our test resources anyway)
'PrestaShop SA and Contributors',
];

yield 'existing-headers-discrimination' => [
Expand All @@ -213,6 +223,7 @@ public static function getFoldersToTest(): iterable
[
'existing-headers-discrimination.php',
],
'friendsofpresta',
];

yield 'smart-headers' => [
Expand Down