Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.62% covered (warning)
84.62%
11 / 13
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
RuntimeEnvironment
84.62% covered (warning)
84.62%
11 / 13
66.67% covered (warning)
66.67%
4 / 6
9.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isGithubActions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCi
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isComposerTestRun
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 isAgentPresent
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
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\Environment;
21
22use Ergebnis\AgentDetector\Detector;
23
24/**
25 * Resolves common runtime-environment flags used by DevTools integrations.
26 */
27  class RuntimeEnvironment implements RuntimeEnvironmentInterface
28{
29    /**
30     * @param EnvironmentInterface $environment reads raw process environment variables
31     * @param Detector $agentDetector detects known AI-agent environment markers
32     */
33    public function __construct(
34        private EnvironmentInterface $environment,
35        private Detector $agentDetector,
36    ) {}
37
38    /**
39     * Returns whether a truthy environment flag is enabled.
40     *
41     * @param string $name the environment variable name
42     */
43    public function isEnabled(string $name): bool
44    {
45        return \in_array(strtolower((string) $this->environment->get($name, '')), ['1', 'true', 'yes', 'on'], true);
46    }
47
48    /**
49     * Returns whether the current process runs in GitHub Actions.
50     */
51    public function isGithubActions(): bool
52    {
53        return $this->isEnabled('GITHUB_ACTIONS');
54    }
55
56    /**
57     * Returns whether the current process runs in a CI environment.
58     */
59    public function isCi(): bool
60    {
61        if ($this->isGithubActions()) {
62            return true;
63        }
64
65        return $this->isEnabled('CI');
66    }
67
68    /**
69     * Returns whether the current process runs inside the Composer or PHPUnit test runtime.
70     */
71    public function isComposerTestRun(): bool
72    {
73        if (\defined('PHPUNIT_COMPOSER_INSTALL')) {
74            return true;
75        }
76
77        return $this->isEnabled('COMPOSER_TESTS_ARE_RUNNING');
78    }
79
80    /**
81     * Returns whether the current process exposes known AI-agent environment markers.
82     */
83    public function isAgentPresent(): bool
84    {
85        $environment = $this->environment->get();
86
87        if (! \is_array($environment)) {
88            return false;
89        }
90
91        return $this->agentDetector->isAgentPresent($environment);
92    }
93}