Skip to content

Commit 842af32

Browse files
committed
Apply fixes from StyleCI
1 parent 25f5c7f commit 842af32

6 files changed

Lines changed: 122 additions & 105 deletions

File tree

src/Conso/Commands/Compile.php

Lines changed: 98 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,204 @@
1-
<?php namespace Conso\Commands;
1+
<?php
2+
3+
namespace Conso\Commands;
24

35
/**
46
* @author <contact@lotfio.net>
5-
* @package Conso PHP Console Creator
7+
*
68
* @version 2.0.0
9+
*
710
* @license MIT
11+
*
812
* @category CLI
13+
*
914
* @copyright 2019 Lotfio Lakehal
1015
*/
1116

1217
use Conso\Command;
13-
use Conso\Exceptions\{CompileException,InputException};
14-
use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface};
18+
use Conso\Contracts\CommandInterface;
19+
use Conso\Contracts\InputInterface;
20+
use Conso\Contracts\OutputInterface;
21+
use Conso\Exceptions\CompileException;
22+
use Conso\Exceptions\InputException;
1523

1624
class Compile extends Command implements CommandInterface
1725
{
1826
/**
19-
* sub commands
27+
* sub commands.
2028
*
2129
* @var array
2230
*/
2331
protected $sub = [
24-
'init'
32+
'init',
2533
];
2634

2735
/**
28-
* flags
36+
* flags.
2937
*
3038
* @var array
3139
*/
3240
protected $flags = [
33-
'--no-shebang'
41+
'--no-shebang',
3442
];
3543

3644
/**
37-
* command help
45+
* command help.
3846
*
3947
* @var array
4048
*/
41-
protected $help = [
49+
protected $help = [
4250
'sub' => [
43-
'init' => 'initialize .json build file'
51+
'init' => 'initialize .json build file',
4452
],
4553
'flags' => [
46-
'--no-shebang' => 'no stub shebang, usefull when invoking phar from http'
47-
]
54+
'--no-shebang' => 'no stub shebang, usefull when invoking phar from http',
55+
],
4856
];
4957

5058
/**
51-
* command description
59+
* command description.
5260
*
5361
* @var string
5462
*/
5563
protected $description = 'Compile your package to a shareable phar file.';
5664

5765
/**
58-
* set up command dependencies
66+
* set up command dependencies.
5967
*/
6068
public function __construct()
6169
{
6270
$this->cwd = getcwd();
63-
$this->packageFile = $this->cwd . '/conso.json';
71+
$this->packageFile = $this->cwd.'/conso.json';
6472
}
6573

6674
/**
67-
* execute method
75+
* execute method.
76+
*
77+
* @param InputInterface $input
78+
* @param OutputInterface $output
6879
*
69-
* @param InputInterface $input
70-
* @param OutputInterface $output
7180
* @return void
7281
*/
73-
public function execute(InputInterface $input, OutputInterface $output) : void
82+
public function execute(InputInterface $input, OutputInterface $output): void
7483
{
75-
if(!\file_exists($this->packageFile))
84+
if (!\file_exists($this->packageFile)) {
7685
throw new CompileException("package file ($this->packageFile) not found");
77-
78-
if(ini_get('phar.readonly') == 1)
79-
throw new CompileException("phar is read only mode ! it should be turned off from php.init to compile.");
80-
86+
}
87+
if (ini_get('phar.readonly') == 1) {
88+
throw new CompileException('phar is read only mode ! it should be turned off from php.init to compile.');
89+
}
8190
// read package file
8291
$buildFile = (array) json_decode(file_get_contents($this->packageFile));
8392

8493
// validate build file
8594
$this->validateBuildFile($buildFile);
86-
95+
8796
// create a phar file if everything went well
8897
$shebang = ($input->flag('--no-shebang') === false) ? true : false;
89-
if($this->createPhar($buildFile, $shebang))
98+
if ($this->createPhar($buildFile, $shebang)) {
9099
exit($output->writeLn("\npackage compiled successfully .\n\n", 'green'));
91-
100+
}
101+
92102
$output->writeLn("error compiling package.\n", 'red');
93103
}
94104

95105
/**
96-
* init package to compile
106+
* init package to compile.
97107
*
98108
* @return void
99109
*/
100-
public function init(InputInterface $input, OutputInterface $output) : void
110+
public function init(InputInterface $input, OutputInterface $output): void
101111
{
102-
if(!\is_writable($this->cwd))
112+
if (!\is_writable($this->cwd)) {
103113
throw new InputException("{$this->cwd} is not writable.");
104-
105-
if(\file_exists($this->packageFile))
114+
}
115+
if (\file_exists($this->packageFile)) {
106116
throw new InputException("build file ({$this->packageFile}) exists already.");
107-
117+
}
108118
$content = [
109-
"src" => [
110-
"src/Conso",
111-
"vendor"
119+
'src' => [
120+
'src/Conso',
121+
'vendor',
112122
],
113-
"build" => "build",
114-
"stub" => "conso",
115-
"phar" => "conso.phar"
123+
'build' => 'build',
124+
'stub' => 'conso',
125+
'phar' => 'conso.phar',
116126
];
117127

118-
if(\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT)))
128+
if (\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT))) {
119129
exit($output->writeLn("\nbuild file created successfully.\n\n", 'green'));
130+
}
120131

121132
$output->writeLn("error creating build file.\n", 'red');
122133
}
123134

124135
/**
125-
* validate build file
136+
* validate build file.
126137
*
127138
* @param string
139+
*
128140
* @return void
129141
*/
130-
private function validateBuildFile(array $file) : void
142+
private function validateBuildFile(array $file): void
131143
{
132-
if(!is_array($file) || count($file) < 4)
133-
throw new CompileException("build file is not a valid json file.");
134-
135-
if(!in_array('src', array_keys($file)))
136-
throw new CompileException("source (src) directory is missing from package file.");
137-
138-
if(!in_array('build', array_keys($file)))
139-
throw new CompileException("build (build) directory is missing from package file.");
140-
141-
if(!in_array('stub', array_keys($file)))
142-
throw new CompileException("stub (stub) file is missing from package file.");
143-
144-
if(!in_array('phar', array_keys($file)))
145-
throw new CompileException("output (phar) file is missing from package file.");
146-
147-
if(!is_array($file['src']))
148-
throw new CompileException("source (src) directory must be an array of valid directories.");
149-
150-
foreach($file['src'] as $dir)
151-
if(!is_dir($dir))
144+
if (!is_array($file) || count($file) < 4) {
145+
throw new CompileException('build file is not a valid json file.');
146+
}
147+
if (!in_array('src', array_keys($file))) {
148+
throw new CompileException('source (src) directory is missing from package file.');
149+
}
150+
if (!in_array('build', array_keys($file))) {
151+
throw new CompileException('build (build) directory is missing from package file.');
152+
}
153+
if (!in_array('stub', array_keys($file))) {
154+
throw new CompileException('stub (stub) file is missing from package file.');
155+
}
156+
if (!in_array('phar', array_keys($file))) {
157+
throw new CompileException('output (phar) file is missing from package file.');
158+
}
159+
if (!is_array($file['src'])) {
160+
throw new CompileException('source (src) directory must be an array of valid directories.');
161+
}
162+
foreach ($file['src'] as $dir) {
163+
if (!is_dir($dir)) {
152164
throw new CompileException("source ($dir) directory is not a valid directory.");
153-
154-
if(!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build']))
165+
}
166+
}
167+
if (!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build'])) {
155168
throw new CompileException("build ({$file['build']}) directory is not a valid directory.");
156-
157-
if(!is_string($file['stub']) || !file_exists($file['stub']))
169+
}
170+
if (!is_string($file['stub']) || !file_exists($file['stub'])) {
158171
throw new CompileException("stub file ({$file['stub']}) not found.");
159-
160-
if(!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false)
172+
}
173+
if (!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false) {
161174
throw new CompileException("phar file ({$file['phar']}) must be a valid file ends with .phar extension.");
175+
}
162176
}
163177

164178
/**
165-
* create a phar
179+
* create a phar.
180+
*
181+
* @param array $rules
166182
*
167-
* @param array $rules
168183
* @return void
169184
*/
170-
private function createPhar(array $rules, bool $shebang = false) : bool
185+
private function createPhar(array $rules, bool $shebang = false): bool
171186
{
172-
$buildLocation = rtrim($rules['build'], '/') . "/package/";
187+
$buildLocation = rtrim($rules['build'], '/').'/package/';
173188

174189
// delete old build files if any
175190
deleteTree($buildLocation);
176191

177192
// copy project
178-
foreach($rules['src'] as $src)
179-
copyDirectory($src, $buildLocation . rtrim($src, '/'));
193+
foreach ($rules['src'] as $src) {
194+
copyDirectory($src, $buildLocation.rtrim($src, '/'));
195+
}
180196

181197
// copy stub file
182-
copy($rules['stub'], $buildLocation . $rules['stub']);
198+
copy($rules['stub'], $buildLocation.$rules['stub']);
183199

184200
// create phar
185-
$phar = new \Phar(rtrim($rules['build'], '/') . "/" . $rules['phar']);
201+
$phar = new \Phar(rtrim($rules['build'], '/').'/'.$rules['phar']);
186202

187203
// start buffering. Mandatory to modify stub to add shebang
188204
$phar->startBuffering();
@@ -194,7 +210,7 @@ private function createPhar(array $rules, bool $shebang = false) : bool
194210
$phar->buildFromDirectory($buildLocation);
195211

196212
// add shebang
197-
$stub = "#!/usr/bin/env php \n" . $defaultStub;
213+
$stub = "#!/usr/bin/env php \n".$defaultStub;
198214
$phar->setStub($shebang === true ? $stub : $defaultStub);
199215

200216
// stop buffering
@@ -203,8 +219,9 @@ private function createPhar(array $rules, bool $shebang = false) : bool
203219
// gzip compressin
204220
$phar->compressFiles(\Phar::GZ);
205221

206-
# Make the file executable
207-
chmod(rtrim($rules['build'], '/') . "/" . $rules['phar'], 0770);
222+
// Make the file executable
223+
chmod(rtrim($rules['build'], '/').'/'.$rules['phar'], 0770);
224+
208225
return deleteTree($buildLocation);
209226
}
210-
}
227+
}

src/Conso/Testing/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public function setUp(): void
3737
$this->output->disableAnsi();
3838
$this->output->enableTestMode();
3939
}
40-
}
40+
}

src/Conso/hlprs.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,46 +117,49 @@ function commandHelp(array $command, $output)
117117
}
118118

119119
/**
120-
* copy directory recursively
120+
* copy directory recursively.
121121
*
122122
* @param string $source
123123
* @param string $destination
124+
*
124125
* @return void
125126
*/
126-
function copyDirectory(string $source, string $destination) : void
127+
function copyDirectory(string $source, string $destination): void
127128
{
128129
mkdir($destination, 0755, true);
129130

130131
foreach (
131132
$iterator = new \RecursiveIteratorIterator(
132133
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
133-
\RecursiveIteratorIterator::SELF_FIRST) as $item
134-
)
135-
{
134+
\RecursiveIteratorIterator::SELF_FIRST
135+
) as $item
136+
) {
136137
if ($item->isDir()) {
137-
mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
138+
mkdir($destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
138139
} else {
139-
copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
140+
copy($item, $destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
140141
}
141142
}
142143
}
143144

144145
/**
145-
* delete directory recursively
146+
* delete directory recursively.
146147
*
147148
* @param string $dir
149+
*
148150
* @return void
149151
*/
150152
function deleteTree(string $dir)
151-
{
152-
if(!is_dir($dir))
153+
{
154+
if (!is_dir($dir)) {
153155
return false;
154-
155-
$files = array_diff(scandir($dir), array('.', '..'));
156+
}
157+
158+
$files = array_diff(scandir($dir), ['.', '..']);
156159

157-
foreach ($files as $file) {
158-
(is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file");
160+
foreach ($files as $file) {
161+
(is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file");
159162
}
160163

161-
return rmdir($dir);
162-
}
164+
return rmdir($dir);
165+
}

tests/Unit/CommandInvokerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* @copyright 2019 Lotfio Lakehal
1515
*/
1616

17+
use Conso\CommandInvoker;
1718
use Conso\Conso;
1819
use Conso\Input;
19-
use Conso\Output;
20-
use Conso\CommandInvoker;
2120
use Conso\Testing\TestCase;
2221

2322
class CommandInvokerTest extends TestCase
@@ -37,7 +36,7 @@ class CommandInvokerTest extends TestCase
3736
public function setUp(): void
3837
{
3938
parent::setUp();
40-
39+
4140
$this->commands = [
4241
[
4342
'name' => 'make',

0 commit comments

Comments
 (0)