Skip to content

Commit 84ade71

Browse files
committed
Refactor to collections
1 parent ba746e3 commit 84ade71

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

cli/Valet/Brew.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,19 @@ function installed($formula)
4040
*/
4141
function hasInstalledPhp()
4242
{
43-
return $this->installed('php71')
44-
|| $this->installed('php70')
45-
|| $this->installed('php56');
43+
return $this->supportedPhpVersions()->contains(function ($version) {
44+
return $this->installed($version);
45+
});
46+
}
47+
48+
/**
49+
* Get a list of supported PHP versions
50+
*
51+
* @return \Illuminate\Support\Collection
52+
*/
53+
function supportedPhpVersions()
54+
{
55+
return collect(['php72', 'php71', 'php70', 'php56']);
4656
}
4757

4858
/**
@@ -171,15 +181,11 @@ function linkedPhp()
171181

172182
$resolvedPath = $this->files->readLink('/usr/local/bin/php');
173183

174-
// Check all currently supported PHP versions
175-
$phpVersions = ['php72', 'php71', 'php70', 'php56'];
176-
foreach ($phpVersions as $version) {
177-
if (strpos($resolvedPath, $version) !== false) {
178-
return $version;
179-
}
180-
}
181-
182-
throw new DomainException("Unable to determine linked PHP.");
184+
return $this->supportedPhpVersions()->first(function ($version) use ($resolvedPath) {
185+
return strpos($resolvedPath, $version) !== false;
186+
}, function () {
187+
throw new DomainException("Unable to determine linked PHP.");
188+
});
183189
}
184190

185191
/**

tests/BrewTest.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,38 @@ public function test_installed_returns_false_when_given_formula_is_not_installed
6666
public function test_has_installed_php_indicates_if_php_is_installed_via_brew()
6767
{
6868
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
69-
$brew->shouldReceive('installed')->once()->with('php71')->andReturn(true);
70-
$brew->shouldReceive('installed')->with('php70')->andReturn(true);
71-
$brew->shouldReceive('installed')->with('php56')->andReturn(true);
69+
$brew->shouldReceive('installed')->with('php72')->andReturn(true);
70+
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
71+
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
72+
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
73+
$this->assertTrue($brew->hasInstalledPhp());
74+
75+
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
76+
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
77+
$brew->shouldReceive('installed')->with('php71')->andReturn(true);
78+
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
79+
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
7280
$this->assertTrue($brew->hasInstalledPhp());
7381

7482
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
75-
$brew->shouldReceive('installed')->once()->with('php70')->andReturn(true);
83+
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
7684
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
85+
$brew->shouldReceive('installed')->with('php70')->andReturn(true);
7786
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
7887
$this->assertTrue($brew->hasInstalledPhp());
7988

8089
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
81-
$brew->shouldReceive('installed')->once()->with('php71')->andReturn(false);
82-
$brew->shouldReceive('installed')->once()->with('php70')->andReturn(false);
83-
$brew->shouldReceive('installed')->once()->with('php56')->andReturn(false);
90+
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
91+
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
92+
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
93+
$brew->shouldReceive('installed')->with('php56')->andReturn(true);
94+
$this->assertTrue($brew->hasInstalledPhp());
95+
96+
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
97+
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
98+
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
99+
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
100+
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
84101
$this->assertFalse($brew->hasInstalledPhp());
85102
}
86103

0 commit comments

Comments
 (0)