Skip to content

Commit f00e549

Browse files
committed
Add mode to prevent bool arguments
1 parent f403c4f commit f00e549

File tree

4 files changed

+86
-28
lines changed

4 files changed

+86
-28
lines changed

src/Script/Configuration.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,37 @@ class Configuration
2525
*/
2626
private $composerBinDirectory;
2727

28-
/**
29-
* @var bool
30-
*/
31-
private $isDevMode = true;
32-
33-
/**
34-
* @var bool
35-
*/
36-
private $isInteractiveMode = true;
37-
3828
/**
3929
* @param Composer $composer
40-
* @param bool $isDevMode
41-
* @param bool $isInteractiveMode
30+
* @param Mode $mode
4231
*/
43-
public function __construct(Composer $composer, $isDevMode = true, $isInteractiveMode = true)
32+
public function __construct(Composer $composer, Mode $mode)
4433
{
4534
$extras = $composer->getPackage()->getExtra();
4635

4736
if (true === array_key_exists('tools', $extras)) {
4837
$this->data = array_merge([], $extras['tools']);
4938
}
5039

40+
$this->mode = $mode;
5141
$this->binDirectory = realpath(__DIR__ . '/../../bin');
5242
$this->composerBinDirectory = $composer->getConfig()->get('bin-dir');
53-
$this->isDevMode = $isDevMode;
54-
$this->isInteractiveMode = $isInteractiveMode;
5543
}
5644

5745
/**
5846
* @return bool
5947
*/
6048
public function isDevMode()
6149
{
62-
return $this->isDevMode;
50+
return $this->mode->isDev();
6351
}
6452

6553
/**
6654
* @return bool
6755
*/
6856
public function isInteractiveMode()
6957
{
70-
return $this->isInteractiveMode;
58+
return $this->mode->isInteractive();
7159
}
7260

7361
/**
@@ -88,6 +76,7 @@ public function getComposerBinDirectory()
8876

8977
/**
9078
* @return array
79+
* @SuppressWarnings(PHPMD.StaticAccess)
9180
*/
9281
public function getTools()
9382
{

src/Script/Mode.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Tooly\Script;
4+
5+
/**
6+
* @package Tooly\Script
7+
*/
8+
class Mode
9+
{
10+
/**
11+
* @var bool
12+
*/
13+
private $isDev = true;
14+
15+
/**
16+
* @var bool
17+
*/
18+
private $isInteractive = true;
19+
20+
/**
21+
* Set flag for composer dev-mode to false.
22+
*/
23+
public function setNoDev()
24+
{
25+
$this->isDev = false;
26+
}
27+
28+
/**
29+
* Set flag for CLI interaction to false.
30+
*/
31+
public function setNonInteractive()
32+
{
33+
$this->isInteractive = false;
34+
}
35+
36+
/**
37+
* Returns if composer runs in dev-mode.
38+
*
39+
* @return bool
40+
*/
41+
public function isDev()
42+
{
43+
return $this->isDev;
44+
}
45+
46+
/**
47+
* Returns if the CLI can interact.
48+
*
49+
* @return bool
50+
*/
51+
public function isInteractive()
52+
{
53+
return $this->isInteractive;
54+
}
55+
}

src/ScriptHandler.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tooly\Script\Helper\Filesystem;
99
use Tooly\Script\Helper\Downloader;
1010
use Tooly\Script\Helper\Verifier;
11+
use Tooly\Script\Mode;
1112
use Tooly\Script\Processor;
1213
use TM\GPG\Verification\Verifier as GPGVerifier;
1314

@@ -22,11 +23,17 @@ class ScriptHandler
2223
public static function installPharTools(Event $event)
2324
{
2425
$gpgVerifier = null;
25-
$configuration = new Configuration(
26-
$event->getComposer(),
27-
$event->isDevMode(),
28-
$event->getIO()->isInteractive()
29-
);
26+
$mode = new Mode;
27+
28+
if (false === $event->isDevMode()) {
29+
$mode->setNoDev();
30+
}
31+
32+
if (false === $event->getIO()->isInteractive()) {
33+
$mode->setNonInteractive();
34+
}
35+
36+
$configuration = new Configuration($event->getComposer(), $mode);
3037

3138
if (true === class_exists(GPGVerifier::class)) {
3239
$gpgVerifier = new GPGVerifier;

tests/Script/ConfigurationTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Composer\Config;
77
use Composer\Package\Package;
88
use Tooly\Script\Configuration;
9+
use Tooly\Script\Mode;
910

1011
/**
1112
* @package Tooly\Tests\Script
@@ -14,7 +15,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
1415
{
1516
public function testIfNoToolsSetEmptyToolSetIsGiven()
1617
{
17-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''));
18+
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode(true, false));
1819
$this->assertCount(0, $configuration->getTools());
1920
}
2021

@@ -28,31 +29,37 @@ public function testCanGetCorrectToolSet()
2829
]
2930
];
3031

31-
$configuration = new Configuration($this->getPreparedComposerInstance($extra, ''));
32+
$configuration = new Configuration($this->getPreparedComposerInstance($extra, ''), new Mode);
3233
$this->assertCount(1, $configuration->getTools());
3334
}
3435

3536
public function testCanCheckDevMode()
3637
{
37-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''));
38+
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode);
3839
$this->assertTrue($configuration->isDevMode());
3940
}
4041

4142
public function testCanSetDevMode()
4243
{
43-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), false);
44+
$mode = new Mode;
45+
$mode->setNoDev();
46+
47+
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), $mode);
4448
$this->assertFalse($configuration->isDevMode());
4549
}
4650

4751
public function testCanCheckInteractiveMode()
4852
{
49-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''));
53+
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode);
5054
$this->assertTrue($configuration->isInteractiveMode());
5155
}
5256

5357
public function testCanSetInteractiveMode()
5458
{
55-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), true, false);
59+
$mode = new Mode;
60+
$mode->setNonInteractive();
61+
62+
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), $mode);
5663
$this->assertFalse($configuration->isInteractiveMode());
5764
}
5865

0 commit comments

Comments
 (0)