• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / composer-update-check / 10285821446

07 Aug 2024 02:02PM UTC coverage: 25.504%. First build
10285821446

Pull #130

github

web-flow
Merge ed00778d6 into 7d4f7bd74
Pull Request #130: [!!!][FEATURE] Modernize plugin

328 of 1362 new or added lines in 47 files covered. (24.08%)

354 of 1388 relevant lines covered (25.5%)

1.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Configuration/Adapter/EnvironmentVariablesConfigAdapter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/composer-update-check".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\ComposerUpdateCheck\Configuration\Adapter;
25

26
use EliasHaeussler\ComposerUpdateCheck\Configuration;
27
use Generator;
28

29
use function array_filter;
30
use function explode;
31
use function getenv;
32
use function preg_match;
33
use function sprintf;
34
use function str_starts_with;
35
use function strtolower;
36
use function trim;
37

38
/**
39
 * EnvironmentVariablesConfigAdapter.
40
 *
41
 * @author Elias Häußler <elias@haeussler.dev>
42
 * @license GPL-3.0-or-later
43
 */
44
final class EnvironmentVariablesConfigAdapter implements ConfigAdapter
45
{
46
    private const ENV_VAR_PREFIX = 'COMPOSER_UPDATE_CHECK_';
47

NEW
48
    public function resolve(): Configuration\ComposerUpdateCheckConfig
×
49
    {
NEW
50
        $config = new Configuration\ComposerUpdateCheckConfig();
×
51

52
        // Exclude patterns
NEW
53
        $excludePatterns = $this->getEnv('EXCLUDE_PATTERNS', true);
×
NEW
54
        foreach ($excludePatterns as $excludePattern) {
×
NEW
55
            $config->excludePackageByPattern(Configuration\Options\PackageExcludePattern::create($excludePattern));
×
56
        }
57

58
        // Include dev packages
NEW
59
        $noDev = $this->getEnv('NO_DEV');
×
NEW
60
        if ($this->isTrue($noDev)) {
×
NEW
61
            $config->excludeDevPackages();
×
62
        }
63

64
        // Perform security scan
NEW
65
        $securityScan = $this->getEnv('SECURITY_SCAN');
×
NEW
66
        if ($this->isTrue($securityScan)) {
×
NEW
67
            $config->performSecurityScan();
×
68
        }
69

70
        // Format
NEW
71
        $format = $this->getEnv('FORMAT');
×
NEW
72
        if (null !== $format) {
×
NEW
73
            $config->setFormat($format);
×
74
        }
75

76
        // Reporters
NEW
77
        foreach ($this->getReporterEnvVariables() as $name => $reporterConfig) {
×
NEW
78
            if ($reporterConfig['enable']) {
×
NEW
79
                $config->enableReporter($name, $reporterConfig['options'] ?? []);
×
80
            } else {
NEW
81
                $config->disableReporter($name);
×
82
            }
83
        }
84

NEW
85
        return $config;
×
86
    }
87

88
    /**
89
     * @phpstan-return ($list is true ? array<string> : string|null)
90
     */
NEW
91
    private function getEnv(string $name, bool $list = false): string|array|null
×
92
    {
NEW
93
        $value = getenv(self::ENV_VAR_PREFIX.$name);
×
94

NEW
95
        if (false === $value || '' === trim($value)) {
×
NEW
96
            return $list ? [] : null;
×
97
        }
98

NEW
99
        if (!$list) {
×
NEW
100
            return $value;
×
101
        }
102

NEW
103
        return explode(',', $value);
×
104
    }
105

106
    /**
107
     * @return Generator<string, array{enable: bool, options?: array<string, mixed>}>
108
     */
NEW
109
    private function getReporterEnvVariables(): Generator
×
110
    {
NEW
111
        $reporterConfig = [];
×
NEW
112
        $envVarPrefix = self::ENV_VAR_PREFIX.'REPORTER_';
×
NEW
113
        $envVarPattern = sprintf('/^%s(?P<name>[^_]+)_(?P<option>.+)$/', $envVarPrefix);
×
114

115
        // Fetch all relevant environment variables
NEW
116
        $envVariables = array_filter(
×
NEW
117
            getenv(),
×
NEW
118
            static fn (string $name) => str_starts_with($name, $envVarPrefix),
×
NEW
119
            ARRAY_FILTER_USE_KEY,
×
NEW
120
        );
×
121

122
        // Resolve reporter config from environment variables
NEW
123
        foreach ($envVariables as $name => $value) {
×
NEW
124
            if (1 !== preg_match($envVarPattern, $name, $matches)) {
×
NEW
125
                continue;
×
126
            }
127

NEW
128
            $reporterName = strtolower($matches['name']);
×
NEW
129
            $reporterOption = strtolower($matches['option']);
×
130

NEW
131
            $reporterConfig[$reporterName][$reporterOption] ??= $value;
×
132
        }
133

134
        // Yield reporter config
NEW
135
        foreach ($reporterConfig as $name => $config) {
×
NEW
136
            $enabled = $this->isTrue($config['enable'] ?? '1');
×
137

NEW
138
            unset($config['enable']);
×
139

NEW
140
            if ($enabled) {
×
NEW
141
                yield $name => [
×
NEW
142
                    'enable' => true,
×
NEW
143
                    'options' => $config,
×
NEW
144
                ];
×
145
            } else {
NEW
146
                yield $name => [
×
NEW
147
                    'enable' => false,
×
NEW
148
                ];
×
149
            }
150
        }
151
    }
152

NEW
153
    private function isTrue(string|null $value): bool
×
154
    {
NEW
155
        if (null === $value) {
×
NEW
156
            return false;
×
157
        }
158

NEW
159
        $value = trim($value);
×
160

NEW
161
        if ('true' === $value || 'yes' === $value) {
×
NEW
162
            return true;
×
163
        }
164

NEW
165
        if ('false' === $value || 'no' === $value) {
×
NEW
166
            return false;
×
167
        }
168

NEW
169
        return (bool) $value;
×
170
    }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc