Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit be4f6a3

Browse files
authored
changed \n to PHP_EOL (#89)
| Q | A | --------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT | Doc PR | -
1 parent 80a50e9 commit be4f6a3

24 files changed

+164
-121
lines changed

.php_cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use Narrowspark\CS\Config\Config;
44
$config = new Config(null, [
55
'native_function_invocation' => [
66
'exclude' => [
7-
'getcwd'
7+
'getcwd',
88
],
99
],
10+
'comment_to_phpdoc' => false,
1011
]);
1112
$config->getFinder()
1213
->files()

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require-dev": {
3535
"composer/composer": "^1.6.0 || ^1.7.0",
3636
"mockery/mockery": "^1.0.0",
37-
"narrowspark/coding-standard": "^1.2.0",
37+
"narrowspark/coding-standard": "^1.2.1",
3838
"narrowspark/testing-helper": "^7.0.0",
3939
"nyholm/nsa": "^1.1.0",
4040
"phpunit/phpunit": "^7.2.0"

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ parameters:
2525
- '#Parameter \#1 \$stream of class Symfony\\Component\\Console\\Output\\StreamOutput constructor expects resource, resource\|false given#'
2626

2727
- '#Call to an undefined method Composer\\DependencyResolver\\Operation\\OperationInterface\:\:getPackage#'
28+
- '#Call to function method_exists\(\) with Symfony\\Component\\Filesystem\\Filesystem and #'
2829

2930
# Automatic
3031
- '#does not call parent constructor from Composer\\Repository\\ComposerRepository#'

src/Automatic/Automatic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public function onPostUpdate(Event $event, array $operations = []): void
632632
'',
633633
'<info>Some files may have been created or updated to configure your new packages.</info>',
634634
'Please <comment>review</comment>, <comment>edit</comment> and <comment>commit</comment> them: these files are <comment>yours</comment>',
635-
"\nTo show the package suggests run <comment>composer suggests</comment>"
635+
\PHP_EOL . 'To show the package suggests run <comment>composer suggests</comment>'
636636
);
637637
}
638638

src/Automatic/Configurator/ComposerAutoScriptsConfigurator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ private function manipulateAndWrite(array $autoScripts): void
9292
{
9393
$this->manipulator->addSubNode('scripts', 'auto-scripts', $autoScripts);
9494

95-
\file_put_contents($this->json->getPath(), $this->manipulator->getContents());
95+
$this->filesystem->dumpFile($this->json->getPath(), $this->manipulator->getContents());
9696
}
9797
}

src/Automatic/Configurator/ComposerScriptsConfigurator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,6 @@ private function manipulateAndWrite(array $scripts): void
221221
{
222222
$this->manipulator->addMainKey('scripts', $scripts);
223223

224-
\file_put_contents($this->json->getPath(), $this->manipulator->getContents());
224+
$this->filesystem->dumpFile($this->json->getPath(), $this->manipulator->getContents());
225225
}
226226
}

src/Automatic/Configurator/EnvConfigurator.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
66
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
77
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
8+
use Narrowspark\Automatic\Configurator\Traits\AppendToFileTrait;
89

910
final class EnvConfigurator extends AbstractConfigurator
1011
{
12+
use AppendToFileTrait;
13+
1114
/**
1215
* {@inheritdoc}
1316
*/
@@ -32,8 +35,8 @@ public function configure(PackageContract $package): void
3235
$data = '';
3336

3437
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $key => $value) {
35-
if ($key[0] === '#' && \is_numeric(\mb_substr($key, 1))) {
36-
$data .= '# ' . $value . "\n";
38+
if (\mb_strpos($key, '#') === 0 && \is_numeric(\mb_substr($key, 1))) {
39+
$data .= '# ' . $value . \PHP_EOL;
3740

3841
continue;
3942
}
@@ -48,7 +51,7 @@ public function configure(PackageContract $package): void
4851
$value = '"' . \str_replace(['\\', '"', "\t", "\n"], ['\\\\', '\\"', '\t', '\n'], $value) . '"';
4952
}
5053

51-
$data .= $key . '=' . $value . "\n";
54+
$data .= $key . '=' . $value . \PHP_EOL;
5255
}
5356

5457
if (! \file_exists($this->path->getWorkingDir() . \DIRECTORY_SEPARATOR . '.env')) {
@@ -57,8 +60,8 @@ public function configure(PackageContract $package): void
5760

5861
$data = $this->markData($package->getPrettyName(), $data);
5962

60-
\file_put_contents($distenv, $data, \FILE_APPEND);
61-
\file_put_contents($this->path->getWorkingDir() . \DIRECTORY_SEPARATOR . '.env', $data, \FILE_APPEND);
63+
$this->appendToFile($distenv, $data);
64+
$this->appendToFile($this->path->getWorkingDir() . \DIRECTORY_SEPARATOR . '.env', $data);
6265
}
6366

6467
/**
@@ -75,10 +78,10 @@ public function unconfigure(PackageContract $package): void
7578
if (! \file_exists($env)) {
7679
continue;
7780
}
78-
/** @codeCoverageIgnoreEnd */
81+
// @codeCoverageIgnoreEnd
7982
$count = 0;
8083
$contents = \preg_replace(
81-
\sprintf('{%s*###> %s ###.*###< %s ###%s+}s', "\n", $package->getPrettyName(), $package->getPrettyName(), "\n"),
84+
\sprintf('{###> %s ###.*###< %s ###%s+}s', $package->getPrettyName(), $package->getPrettyName(), \PHP_EOL),
8285
'',
8386
(string) \file_get_contents($env),
8487
-1,
@@ -93,7 +96,7 @@ public function unconfigure(PackageContract $package): void
9396

9497
$this->write(\sprintf('Removing environment variables from %s', $file));
9598

96-
\file_put_contents($env, $contents);
99+
$this->filesystem->dumpFile($env, $contents);
97100
}
98101
}
99102
}

src/Automatic/Configurator/GitIgnoreConfigurator.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
66
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
77
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
8+
use Narrowspark\Automatic\Configurator\Traits\AppendToFileTrait;
89

910
final class GitIgnoreConfigurator extends AbstractConfigurator
1011
{
12+
use AppendToFileTrait;
13+
1114
/**
1215
* {@inheritdoc}
1316
*/
@@ -33,10 +36,10 @@ public function configure(PackageContract $package): void
3336

3437
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $value) {
3538
$value = self::expandTargetDir($this->options, $value);
36-
$data .= "${value}\n";
39+
$data .= $value . \PHP_EOL;
3740
}
3841

39-
\file_put_contents($gitignore, "\n" . \ltrim($this->markData($package->getPrettyName(), $data), "\r\n"), \FILE_APPEND);
42+
$this->appendToFile($gitignore, \PHP_EOL . \ltrim($this->markData($package->getPrettyName(), $data), "\r\n"));
4043
}
4144

4245
/**
@@ -50,11 +53,11 @@ public function unconfigure(PackageContract $package): void
5053
if (! \file_exists($file)) {
5154
return;
5255
}
53-
/** @codeCoverageIgnoreEnd */
56+
// @codeCoverageIgnoreEnd
5457
$count = 0;
5558
$contents = \preg_replace(
56-
\sprintf('{%s*###> %s ###.*###< %s ###%s+}s', "\n", $package->getPrettyName(), $package->getPrettyName(), "\n"),
57-
"\n",
59+
\sprintf('{###> %s ###.*###< %s ###%s+}s', $package->getPrettyName(), $package->getPrettyName(), \PHP_EOL),
60+
\PHP_EOL,
5861
(string) \file_get_contents($file),
5962
-1,
6063
$count
@@ -66,6 +69,6 @@ public function unconfigure(PackageContract $package): void
6669

6770
$this->write('Removed entries in .gitignore');
6871

69-
\file_put_contents($file, \ltrim($contents, "\r\n"));
72+
$this->filesystem->dumpFile($file, \ltrim($contents, "\r\n"));
7073
}
7174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Narrowspark\Automatic\Configurator\Traits;
4+
5+
use Symfony\Component\Filesystem\Exception\IOException;
6+
7+
trait AppendToFileTrait
8+
{
9+
/**
10+
* Appends content to an existing file.
11+
*
12+
* @see \Symfony\Component\Filesystem\Filesystem::appendToFile()
13+
*
14+
* @param string $filename The file to which to append content
15+
* @param string $content The content to append
16+
*
17+
* @throws IOException If the file is not writable
18+
*
19+
* @return void
20+
*/
21+
public function appendToFile(string $filename, string $content): void
22+
{
23+
if (\method_exists($this->filesystem, 'appendToFile')) {
24+
$this->filesystem->appendToFile($filename, $content);
25+
26+
return;
27+
}
28+
// @codeCoverageIgnoreStart
29+
$dir = \dirname($filename);
30+
31+
if (! \is_dir($dir)) {
32+
$this->filesystem->mkdir($dir);
33+
}
34+
35+
if (! \is_writable($dir)) {
36+
throw new IOException(\sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
37+
}
38+
39+
if (false === @\file_put_contents($filename, $content, \FILE_APPEND)) {
40+
throw new IOException(\sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
41+
}
42+
// @codeCoverageIgnoreEnd
43+
}
44+
}

src/Automatic/Installer/InstallationManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function run(): int
9191
$status = $this->runInstaller($this->rootPackage, $this->whiteList);
9292

9393
if ($status !== 0) {
94-
$this->io->writeError("\n" . '<error>Removal failed, reverting ' . Factory::getComposerFile() . ' to its original content.</error>');
94+
$this->io->writeError(\PHP_EOL . '<error>Removal failed, reverting ' . Factory::getComposerFile() . ' to its original content.</error>');
9595

9696
\file_put_contents($this->jsonFile->getPath(), $this->composerBackup);
9797
}

src/Automatic/QuestionFactory.php

+9-13
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ final class QuestionFactory
1616
*/
1717
public static function getPackageQuestion(string $name, ?string $url): string
1818
{
19-
$message = <<<'PHP'
20-
Do you want to execute this package [%s]?
21-
[<comment>y</comment>] Yes
22-
[<comment>n</comment>] No
23-
[<comment>a</comment>] Yes for all packages, only for the current installation session
24-
[<comment>p</comment>] Yes permanently, never ask again for this project
25-
(defaults to <comment>n</comment>):
26-
PHP;
19+
$message = ' Do you want to execute this package [%s]?' . \PHP_EOL;
20+
$message .= ' [<comment>y</comment>] Yes' . \PHP_EOL;
21+
$message .= ' [<comment>n</comment>] No' . \PHP_EOL;
22+
$message .= ' [<comment>a</comment>] Yes for all packages, only for the current installation session' . \PHP_EOL;
23+
$message .= ' [<comment>p</comment>] Yes permanently, never ask again for this project' . \PHP_EOL;
24+
$message .= ' (defaults to <comment>n</comment>): ' . \PHP_EOL;
2725

2826
if ($url === null) {
2927
return \sprintf($message, $name);
3028
}
3129

32-
return \sprintf(" Review the package from %s.\n" . $message, \str_replace('.git', '', $url), $name);
30+
return \sprintf(' Review the package from %s.' . \PHP_EOL . $message, \str_replace('.git', '', $url), $name);
3331
}
3432

3533
/**
@@ -41,10 +39,8 @@ public static function getPackageQuestion(string $name, ?string $url): string
4139
*/
4240
public static function getPackageScriptsQuestion(string $name): string
4341
{
44-
$message = <<<'PHP'
45-
Do you want to add this package [%s] composer scripts?
46-
(defaults to <comment>no</comment>):
47-
PHP;
42+
$message = ' Do you want to add this package [%s] composer scripts?' . \PHP_EOL;
43+
$message .= ' (defaults to <comment>no</comment>): ' . \PHP_EOL;
4844

4945
return \sprintf($message, $name);
5046
}

src/Automatic/ScriptExecutor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function execute(string $type, string $cmd): void
116116
$this->io->isDecorated()
117117
);
118118

119-
/** @codeCoverageIgnoreStart */
119+
// @codeCoverageIgnoreStart
120120
$outputHandler = function ($type, $buffer) use ($cmdOutput): void {
121121
$cmdOutput->write($buffer, false, OutputInterface::OUTPUT_RAW);
122122
};
@@ -142,7 +142,7 @@ public function execute(string $type, string $cmd): void
142142

143143
\fseek($cmdOutput->getStream(), 0);
144144

145-
foreach (\explode("\n", (string) \stream_get_contents($cmdOutput->getStream())) as $line) {
145+
foreach (\explode(\PHP_EOL, (string) \stream_get_contents($cmdOutput->getStream())) as $line) {
146146
$this->io->writeError('!! ' . $line);
147147
}
148148

src/Automatic/SkeletonGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function run(): void
104104
/** @var \Narrowspark\Automatic\Common\Generator\AbstractGenerator $generator */
105105
$generator = $generators[$answer];
106106

107-
$this->io->write(\sprintf('%sGenerating [%s] skeleton.%s', "\n", $generatorTypes[$answer], "\n"));
107+
$this->io->write(\sprintf('%sGenerating [%s] skeleton.%s', \PHP_EOL, $generatorTypes[$answer], \PHP_EOL));
108108

109109
$generator->generate();
110110

src/Common/Configurator/AbstractConfigurator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function isFileMarked(string $packageName, string $file): bool
106106
*/
107107
protected function markData(string $packageName, string $data, int $spaceMultiplier = 0): string
108108
{
109-
return \sprintf("###> %s ###\n%s\n###< %s ###\n", $packageName, \rtrim($data, "\r\n"), $packageName);
109+
return \sprintf('###> %s ###' . \PHP_EOL . '%s' . \PHP_EOL . '###< %s ###' . \PHP_EOL, $packageName, \rtrim($data, "\r\n"), $packageName);
110110
}
111111

112112
/**

src/Common/Installer/Installer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static function getPreferredInstallOptions(Config $config, InputInterfac
100100
// noop
101101
break;
102102
}
103-
/** @codeCoverageIgnoreEnd */
103+
// @codeCoverageIgnoreEnd
104104
$preferSource = self::getOption($input, 'prefer-source', $preferSource);
105105
$preferDist = self::getOption($input, 'prefer-dist', $preferDist);
106106

src/Common/ScriptExtender/PhpScriptExtender.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function expand(string $cmd): string
2525
if (($php = $phpFinder->find(false)) === false) {
2626
throw new RuntimeException('The PHP executable could not be found, add it to your PATH and try again.');
2727
}
28-
/** @codeCoverageIgnoreEnd */
28+
// @codeCoverageIgnoreEnd
2929
$arguments = $phpFinder->findArguments();
3030

3131
if (($env = \getenv('COMPOSER_ORIGINAL_INIS')) !== false) {

src/Common/Traits/PhpFileMarkerTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ protected function markData(string $packageName, string $data, int $spaceMultipl
3030
{
3131
$spaces = \str_repeat(' ', $spaceMultiplier);
3232

33-
return \sprintf("%s/** > %s **/\n%s%s/** %s < **/\n", $spaces, $packageName, $data, $spaces, $packageName);
33+
return \sprintf('%s/** > %s **/' . \PHP_EOL . '%s%s/** %s < **/' . \PHP_EOL, $spaces, $packageName, $data, $spaces, $packageName);
3434
}
3535
}

src/Security/Command/Formatter/SimpleFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function displayResults(SymfonyStyle $output, array $vulnerabilities): vo
1515
if (\count($vulnerabilities) !== 0) {
1616
foreach ($vulnerabilities as $dependency => $issues) {
1717
$dependencyFullName = $dependency . ' (' . $issues['version'] . ')';
18-
$output->writeln('<info>' . $dependencyFullName . "\n" . \str_repeat('-', \mb_strlen($dependencyFullName)) . "</>\n");
18+
$output->writeln('<info>' . $dependencyFullName . \PHP_EOL . \str_repeat('-', \mb_strlen($dependencyFullName)) . '</>' . \PHP_EOL);
1919

2020
foreach ($issues['advisories'] as $issue => $details) {
2121
$output->write(' * ');

src/Security/Command/Formatter/TextFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function displayResults(SymfonyStyle $output, array $vulnerabilities): vo
1717
$output->section(\sprintf('%s (%s)', $dependency, $issues['version']));
1818

1919
$details = \array_map(function ($value) {
20-
return \sprintf("<info>%s</>: %s\n %s", $value['cve'] ?: '(no CVE ID)', $value['title'], $value['link']);
20+
return \sprintf('<info>%s</>: %s' . \PHP_EOL . ' %s', $value['cve'] ?: '(no CVE ID)', $value['title'], $value['link']);
2121
}, $issues['advisories']);
2222

2323
$output->listing($details);

0 commit comments

Comments
 (0)