Skip to content

Commit

Permalink
Merge pull request #39 from mikeartix/master
Browse files Browse the repository at this point in the history
PHP files: avoid adding unnecessary newline
  • Loading branch information
Quetzacoalt91 authored Mar 23, 2023
2 parents 4bf996e + 5907bf1 commit 3104b69
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Command/UpdateLicensesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,19 @@ private function addLicenseToNode(Stmt $node, SplFileInfo $file): void
{
if (!$node->hasAttribute('comments')) {
$needle = '<?php';
$replace = "<?php\n" . $this->text . "\n";
$replace = "<?php\n" . $this->text;
$haystack = $file->getContents();

$pos = strpos($haystack, $needle);
// Important, if the <?php is in the middle of the file, continue
if ($pos === 0) {
// Check if an empty newline is present right after the <?php tag
// Append newline to replacement if missing
$checkNewline = substr($haystack, strlen($needle), 2) === "\n\n";
if (!$checkNewline) {
$replace .= "\n";
}

$newstring = substr_replace($haystack, $replace, $pos, strlen($needle));

if (!$this->runAsDry) {
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/expected/smart-headers/emptynewlineheader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

class EmptyNewlineHeader extends Module
{
// This PHP file has no header comment but does have an empty newline right after the <?php tag
// Most editors/IDEs add this newline automatically
// It should add the license header without adding an unnecessary extra empty newline
// This also keeps validator.prestashop.com happy :)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class EmptyNewlineHeader extends Module
{
// This PHP file has no header comment but does have an empty newline right after the <?php tag
// Most editors/IDEs add this newline automatically
// It should add the license header without adding an unnecessary extra empty newline
// This also keeps validator.prestashop.com happy :)
}
1 change: 1 addition & 0 deletions tests/integration/runner/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'dashproducts',
'fakemodule',
'existing-headers-discrimination',
'smart-headers',
];
$workspaceID = 100;
$filesystem = new Filesystem();
Expand Down

0 comments on commit 3104b69

Please sign in to comment.