Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkingDirectorySwitcher
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 switchTo
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\SelfUpdate;
21
22use InvalidArgumentException;
23use function Safe\chdir;
24use function Safe\realpath;
25
26/**
27 * Applies Composer-like working-directory switching for the standalone binary.
28 */
29 class WorkingDirectorySwitcher implements WorkingDirectorySwitcherInterface
30{
31    /**
32     * Switches to the provided working directory when one is configured.
33     *
34     * @param string|null $workingDirectory the target working directory, or null when no switch is requested
35     */
36    public function switchTo(?string $workingDirectory): void
37    {
38        if (null === $workingDirectory || '' === $workingDirectory) {
39            return;
40        }
41
42        if (! is_dir($workingDirectory)) {
43            throw new InvalidArgumentException(\sprintf(
44                'The working directory "%s" does not exist.',
45                $workingDirectory
46            ));
47        }
48
49        chdir(realpath($workingDirectory));
50    }
51}