Skip to content

Commit d28f791

Browse files
authored
Merge pull request #78 from pyrech/fix-strpos-null-parameter
Fix PHP 8 warning about passing null to strpos()
2 parents 30bae9a + 47b6924 commit d28f791

File tree

11 files changed

+135
-10
lines changed

11 files changed

+135
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29-
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0']
29+
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
3030
minimum-stability: ['']
3131
composer-flags: ['']
3232
name: ['']

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes between versions
22

3+
## Not yet released
4+
5+
* Fix PHP 8 warning about passing null to strpos() ([#78](https://github.com/pyrech/composer-changelogs/pull/78))
6+
37
## 1.8.1 (2021-10-13)
48

59
* Fix semver output colors ([#74](https://github.com/pyrech/composer-changelogs/pull/74))

src/UrlGenerator/GitBasedUrlGenerator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract protected function getDomain();
3030
*/
3131
public function supports($sourceUrl)
3232
{
33-
return false !== strpos($sourceUrl, $this->getDomain());
33+
return $sourceUrl && false !== strpos($sourceUrl, $this->getDomain());
3434
}
3535

3636
/**
@@ -39,12 +39,16 @@ public function supports($sourceUrl)
3939
* It ensures there is no .git part in http url. It also supports ssh urls
4040
* by converting them in their http equivalent format.
4141
*
42-
* @param string $sourceUrl
42+
* @param ?string $sourceUrl
4343
*
4444
* @return string
4545
*/
4646
protected function generateBaseUrl($sourceUrl)
4747
{
48+
if (!$sourceUrl) {
49+
return '';
50+
}
51+
4852
if ($this->isSshUrl($sourceUrl)) {
4953
return $this->transformSshUrlIntoHttp($sourceUrl);
5054
}

src/UrlGenerator/GithubUrlGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ public function generateReleaseUrl($sourceUrl, Version $version)
6969
return false;
7070
}
7171

72+
$baseUrl = $this->generateBaseUrl($sourceUrl);
73+
74+
if (!$baseUrl) {
75+
return false;
76+
}
77+
7278
return sprintf(
7379
'%s/releases/tag/%s',
74-
$this->generateBaseUrl($sourceUrl),
80+
$baseUrl,
7581
$version->getPretty()
7682
);
7783
}

src/UrlGenerator/GitlabUrlGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function generateReleaseUrl($sourceUrl, Version $version)
7070
return false;
7171
}
7272

73+
$baseUrl = $this->generateBaseUrl($sourceUrl);
74+
75+
if (!$baseUrl) {
76+
return false;
77+
}
78+
7379
return sprintf(
7480
'%s/tags/%s',
7581
$this->generateBaseUrl($sourceUrl),

src/UrlGenerator/UrlGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
interface UrlGenerator
1717
{
1818
/**
19-
* @param string $sourceUrl
19+
* @param ?string $sourceUrl
2020
*
2121
* @return bool
2222
*/
@@ -29,9 +29,9 @@ public function supports($sourceUrl);
2929
* In case the from and to source urls are different, this probably means
3030
* that an across fork compare url should be generated instead.
3131
*
32-
* @param string $sourceUrlFrom
32+
* @param ?string $sourceUrlFrom
3333
* @param Version $versionFrom
34-
* @param string $sourceUrlTo
34+
* @param ?string $sourceUrlTo
3535
* @param Version $versionTo
3636
*
3737
* @return string|false
@@ -42,7 +42,7 @@ public function generateCompareUrl($sourceUrlFrom, Version $versionFrom, $source
4242
* Return the release url for the given version or false if compare url is
4343
* not supported.
4444
*
45-
* @param string $sourceUrl
45+
* @param ?string $sourceUrl
4646
* @param Version $version
4747
*
4848
* @return string|false

src/UrlGenerator/WordPressUrlGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ class WordPressUrlGenerator implements UrlGenerator
2525
*/
2626
public function supports($sourceUrl)
2727
{
28-
return false !== strpos($sourceUrl, self::DOMAIN);
28+
return $sourceUrl && false !== strpos($sourceUrl, self::DOMAIN);
2929
}
3030

3131
/**
3232
* {@inheritdoc}
3333
*/
3434
public function generateCompareUrl($sourceUrlFrom, Version $versionFrom, $sourceUrlTo, Version $versionTo)
3535
{
36+
if (!$sourceUrlTo) {
37+
return false;
38+
}
39+
3640
if (preg_match('#plugins.svn.wordpress.org/(.*)/#', $sourceUrlTo, $matches)) {
3741
$plugin = $matches[1];
3842

tests/UrlGenerator/BitbucketUrlGeneratorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testItDoesNotSupportNonBitbucketUrls()
3636
{
3737
$this->assertFalse($this->SUT->supports('https://github.com/phpunit/phpunit-mock-objects.git'));
3838
$this->assertFalse($this->SUT->supports('https://github.com/symfony/console'));
39+
$this->assertFalse($this->SUT->supports(null));
3940
}
4041

4142
public function testItGeneratesCompareUrlsWithOrWithoutGitExtensionInSourceUrl()
@@ -178,6 +179,21 @@ public function testItGeneratesCompareUrlsWithSshSourceUrl()
178179
);
179180
}
180181

182+
public function testItDoesNotGenerateCompareUrlsWithoutSourceUrl()
183+
{
184+
$versionFrom = new Version('v1.0.0.0', 'v1.0.0', 'v1.0.0');
185+
$versionTo = new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1');
186+
187+
$this->assertFalse(
188+
$this->SUT->generateCompareUrl(
189+
null,
190+
$versionFrom,
191+
null,
192+
$versionTo
193+
)
194+
);
195+
}
196+
181197
public function testItDoesNotGenerateReleaseUrls()
182198
{
183199
$this->assertFalse(
@@ -194,4 +210,14 @@ public function testItDoesNotGenerateReleaseUrls()
194210
)
195211
);
196212
}
213+
214+
public function testItDoesNotGenerateReleaseUrlWithoutSourceUrl()
215+
{
216+
$this->assertFalse(
217+
$this->SUT->generateReleaseUrl(
218+
null,
219+
new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1')
220+
)
221+
);
222+
}
197223
}

tests/UrlGenerator/GithubUrlGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ public function testItGeneratesCompareUrlsWithSshSourceUrl()
178178
);
179179
}
180180

181+
public function testItDoesNotGenerateCompareUrlsWithoutSourceUrl()
182+
{
183+
$versionFrom = new Version('v1.0.0.0', 'v1.0.0', 'v1.0.0');
184+
$versionTo = new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1');
185+
186+
$this->assertFalse(
187+
$this->SUT->generateCompareUrl(
188+
null,
189+
$versionFrom,
190+
null,
191+
$versionTo
192+
)
193+
);
194+
}
195+
181196
public function testItDoesNotGenerateReleaseUrlsForDevVersion()
182197
{
183198
$this->assertFalse(
@@ -224,4 +239,14 @@ public function testItGeneratesReleaseUrlWithSshSourceUrl()
224239
)
225240
);
226241
}
242+
243+
public function testItDoesNotGenerateReleaseUrlWithoutSourceUrl()
244+
{
245+
$this->assertFalse(
246+
$this->SUT->generateReleaseUrl(
247+
null,
248+
new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1')
249+
)
250+
);
251+
}
227252
}

tests/UrlGenerator/GitlabUrlGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ public function testItGeneratesCompareUrlsWithSshSourceUrl()
161161
);
162162
}
163163

164+
public function testItDoesNotGenerateCompareUrlsWithoutSourceUrl()
165+
{
166+
$versionFrom = new Version('v1.0.0.0', 'v1.0.0', 'v1.0.0');
167+
$versionTo = new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1');
168+
169+
$this->assertFalse(
170+
$this->SUT->generateCompareUrl(
171+
null,
172+
$versionFrom,
173+
null,
174+
$versionTo
175+
)
176+
);
177+
}
178+
164179
public function testItDoesNotGenerateReleaseUrlsForDevVersion()
165180
{
166181
$this->assertFalse(
@@ -207,4 +222,14 @@ public function testItGeneratesReleaseUrlWithSshSourceUrl()
207222
)
208223
);
209224
}
225+
226+
public function testItDoesNotGenerateReleaseUrlWithoutSourceUrl()
227+
{
228+
$this->assertFalse(
229+
$this->SUT->generateReleaseUrl(
230+
null,
231+
new Version('v1.0.1.0', 'v1.0.1', 'v1.0.1')
232+
)
233+
);
234+
}
210235
}

0 commit comments

Comments
 (0)