-
-
Notifications
You must be signed in to change notification settings - Fork 101
"Modernize" codebase #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Modernize" codebase #259
Changes from 9 commits
8642447
984d180
164a294
1baa233
55eb1d8
b329976
6250984
e17d9aa
249156a
5bebd41
99284c0
6f5ccfd
95d7137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
errorLevel="1" | ||
resolveFromConfigFile="true" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://getpsalm.org/schema/config" | ||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<ignoreFiles> | ||
<directory name="vendor" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
</psalm> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use Couscous\Generator; | ||
use Couscous\Model\Project; | ||
use Couscous\Model\WatchList\WatchList; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
@@ -34,7 +35,7 @@ public function __construct(Generator $generator) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('preview') | ||
|
@@ -76,25 +77,31 @@ protected function configure() | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
/** @var array */ | ||
$cliConfig = $input->getOption('config'); | ||
|
||
if (!$this->isSupported()) { | ||
$output->writeln('<error>PHP 5.4 or above is required to run the internal webserver</error>'); | ||
$output->writeln('<error>PHP 7.1 or above is required to run the internal webserver</error>'); | ||
|
||
return 1; | ||
} | ||
|
||
/** @var string */ | ||
$sourceDirectory = $input->getArgument('source'); | ||
/** @var string */ | ||
$targetDirectory = $input->getOption('target'); | ||
|
||
if ($input->hasParameterOption('--livereload')) { | ||
if ($input->getOption('livereload') === null) { | ||
$input->setOption('livereload', 'livereload'); | ||
/** @var ?string */ | ||
$livereload = $input->getOption('livereload'); | ||
|
||
if ($livereload === null) { | ||
$livereload = 'livereload'; | ||
} | ||
|
||
if (!$this->isFound($input->getOption('livereload'))) { | ||
if (!$this->isFound($livereload)) { | ||
$output->writeln( | ||
'<error>Impossible to launch Livereload, ' | ||
.'did you forgot to install it with "pip install livereload" (sudo maybe required)?</error>' | ||
|
@@ -103,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
return 1; | ||
} | ||
|
||
$this->startLivereload($input->getOption('livereload'), $output, $sourceDirectory, $targetDirectory); | ||
$this->startLivereload($livereload, $output, $sourceDirectory, $targetDirectory); | ||
} | ||
|
||
$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, $cliConfig); | ||
|
@@ -114,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
if (function_exists('pcntl_signal')) { | ||
declare(ticks=1); | ||
|
||
$handler = function ($signal) use ($serverProcess, $output, &$throwOnServerStop) { | ||
$handler = function (int $signal) use ($serverProcess, $output, &$throwOnServerStop): void { | ||
$throwOnServerStop = !$this->stopWebServer($serverProcess, $output, $signal); | ||
}; | ||
|
||
|
@@ -146,11 +153,11 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
|
||
private function generateWebsite( | ||
OutputInterface $output, | ||
$sourceDirectory, | ||
$targetDirectory, | ||
$cliConfig, | ||
$regenerate = false | ||
) { | ||
string $sourceDirectory, | ||
string $targetDirectory, | ||
array $cliConfig, | ||
bool $regenerate = false | ||
): WatchList { | ||
$project = new Project($sourceDirectory, $targetDirectory); | ||
|
||
$project->metadata['cliConfig'] = $cliConfig; | ||
|
@@ -163,21 +170,23 @@ private function generateWebsite( | |
return $project->watchlist; | ||
} | ||
|
||
private function startWebServer(InputInterface $input, OutputInterface $output, $targetDirectory) | ||
private function startWebServer(InputInterface $input, OutputInterface $output, string $targetDirectory): Process | ||
{ | ||
$processArguments = [PHP_BINARY, '-S', $input->getArgument('address')]; | ||
/** @var string */ | ||
$address = $input->getArgument('address'); | ||
$processArguments = [PHP_BINARY, '-S', $address]; | ||
|
||
$process = new Process($processArguments); | ||
$process->setWorkingDirectory($targetDirectory); | ||
$process->setTimeout(null); | ||
$process->start(); | ||
|
||
$output->writeln(sprintf('Server running on <comment>http://%s</comment>', $input->getArgument('address'))); | ||
$output->writeln(sprintf('Server running on <comment>http://%s</comment>', $address)); | ||
|
||
return $process; | ||
} | ||
|
||
private function stopWebServer(Process $serverProcess, OutputInterface $output, $signal = null) | ||
private function stopWebServer(Process $serverProcess, OutputInterface $output, int $signal = null): bool | ||
{ | ||
$signal = $signal ?: SIGTERM; | ||
|
||
|
@@ -198,8 +207,12 @@ private function stopWebServer(Process $serverProcess, OutputInterface $output, | |
return false; | ||
} | ||
|
||
private function startLivereload($executablePath, OutputInterface $output, $sourceDirectory, $targetDirectory) | ||
{ | ||
private function startLivereload( | ||
string $executablePath, | ||
OutputInterface $output, | ||
string $sourceDirectory, | ||
string $targetDirectory | ||
): void { | ||
$processArguments = [$executablePath, $targetDirectory, '-w', '3']; | ||
|
||
$process = new Process($processArguments); | ||
|
@@ -210,21 +223,21 @@ private function startLivereload($executablePath, OutputInterface $output, $sour | |
$output->writeln('<info>Livereload launched!</info>'); | ||
} | ||
|
||
private function isSupported() | ||
private function isSupported(): bool | ||
{ | ||
if (version_compare(phpversion(), '5.4.0', '<')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That We could completely remove that check now instead of upgrading the error message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if (version_compare(phpversion(), '7.1.0', '<')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function isFound($executablePath) | ||
private function isFound(string $executablePath): bool | ||
{ | ||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | ||
$folders = explode(';', getenv('PATH')); | ||
$folders = explode(';', (string) getenv('PATH')); | ||
} else { | ||
$folders = explode(':', getenv('PATH')); | ||
$folders = explode(':', (string) getenv('PATH')); | ||
} | ||
|
||
foreach ($folders as $folder) { | ||
|
@@ -236,9 +249,9 @@ private function isFound($executablePath) | |
return false; | ||
} | ||
|
||
private function fileListToDisplay(array $files, $sourceDirectory) | ||
private function fileListToDisplay(array $files, string $sourceDirectory): string | ||
{ | ||
$files = array_map(function ($file) use ($sourceDirectory) { | ||
$files = array_map(function (string $file) use ($sourceDirectory): string { | ||
return substr($file, strlen($sourceDirectory) + 1); | ||
}, $files); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to specify this bugfix version as it is the minimum required by
vimeo/psalm