Skip to content

Commit d50e9e4

Browse files
More fixes from the static analyzer
1 parent 92975a6 commit d50e9e4

19 files changed

+132
-227
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Gitlab\Api;
22

33
use Gitlab\Client;
4+
use Gitlab\Exception\RuntimeException;
45
use Gitlab\HttpClient\Message\QueryStringBuilder;
56
use Gitlab\HttpClient\Message\ResponseMediator;
67
use Gitlab\Tests\HttpClient\Message\QueryStringBuilderTest;
@@ -56,8 +57,8 @@ public function configure()
5657
* Performs a GET query and returns the response as a PSR-7 response object.
5758
*
5859
* @param string $path
59-
* @param array $parameters
60-
* @param array $requestHeaders
60+
* @param array<string,mixed> $parameters
61+
* @param array<string,string> $requestHeaders
6162
* @return ResponseInterface
6263
*/
6364
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
@@ -69,8 +70,8 @@ protected function getAsResponse($path, array $parameters = array(), $requestHea
6970

7071
/**
7172
* @param string $path
72-
* @param array $parameters
73-
* @param array $requestHeaders
73+
* @param array<string,mixed> $parameters
74+
* @param array<string,string> $requestHeaders
7475
* @return mixed
7576
*/
7677
protected function get($path, array $parameters = array(), $requestHeaders = array())
@@ -80,9 +81,9 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
8081

8182
/**
8283
* @param string $path
83-
* @param array $parameters
84-
* @param array $requestHeaders
85-
* @param array $files
84+
* @param array<string,mixed> $parameters
85+
* @param array<string,string> $requestHeaders
86+
* @param array<string,string> $files
8687
* @return mixed
8788
*/
8889
protected function post($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
@@ -101,7 +102,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
101102
}
102103

103104
foreach ($files as $name => $file) {
104-
$builder->addResource($name, fopen($file, 'r'), [
105+
$builder->addResource($name, self::tryFopen($file, 'r'), [
105106
'headers' => [
106107
'Content-Type' => $this->guessContentType($file),
107108
],
@@ -120,8 +121,9 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
120121

121122
/**
122123
* @param string $path
123-
* @param array $parameters
124-
* @param array $requestHeaders
124+
* @param array<string,mixed> $parameters
125+
* @param array<string,string> $requestHeaders
126+
* @param array<string,string> $files
125127
* @return mixed
126128
*/
127129
protected function put($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
@@ -140,7 +142,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
140142
}
141143

142144
foreach ($files as $name => $file) {
143-
$builder->addResource($name, fopen($file, 'r'), [
145+
$builder->addResource($name, self::tryFopen($file, 'r'), [
144146
'headers' => [
145147
'Content-Type' => $this->guessContentType($file),
146148
],
@@ -159,8 +161,8 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
159161

160162
/**
161163
* @param string $path
162-
* @param array $parameters
163-
* @param array $requestHeaders
164+
* @param array<string,mixed> $parameters
165+
* @param array<string,string> $requestHeaders
164166
* @return mixed
165167
*/
166168
protected function delete($path, array $parameters = array(), $requestHeaders = array())
@@ -265,6 +267,44 @@ private function guessContentType($file)
265267
}
266268
$finfo = new \finfo(FILEINFO_MIME_TYPE);
267269

268-
return $finfo->file($file);
270+
return $finfo->file($file) ?: 'application/octet-stream';
271+
}
272+
273+
/**
274+
* Safely opens a PHP stream resource using a filename.
275+
*
276+
* When fopen fails, PHP normally raises a warning. This function adds an
277+
* error handler that checks for errors and throws an exception instead.
278+
*
279+
* @param string $filename File to open
280+
* @param string $mode Mode used to open the file
281+
*
282+
* @return resource
283+
*
284+
* @throws RuntimeException if the file cannot be opened
285+
*
286+
* @see https://github.com/guzzle/psr7/blob/1.6.1/src/functions.php#L287-L320
287+
*/
288+
private static function tryFopen($filename, $mode)
289+
{
290+
$ex = null;
291+
set_error_handler(function () use ($filename, $mode, &$ex) {
292+
$ex = new RuntimeException(sprintf(
293+
'Unable to open %s using mode %s: %s',
294+
$filename,
295+
$mode,
296+
func_get_args()[1]
297+
));
298+
});
299+
300+
$handle = fopen($filename, $mode);
301+
restore_error_handler();
302+
303+
if (null !== $ex) {
304+
throw $ex;
305+
}
306+
307+
/** @var resource */
308+
return $handle;
269309
}
270310
}

lib/Gitlab/Api/Groups.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public function create($name, $path, $description = null, $visibility = 'private
5858
'shared_runners_minutes_limit' => $shared_runners_minutes_limit,
5959
);
6060

61-
return $this->post('groups', array_filter($params, 'strlen'));
61+
return $this->post('groups', array_filter($params, function ($value) {
62+
return null !== $value && (!is_string($value) || strlen($value) > 0);
63+
}));
6264
}
6365

6466
/**

lib/Gitlab/Api/IssueBoards.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class IssueBoards extends AbstractApi
44
{
55
/**
6-
* @param int|null $project_id
6+
* @param int|string|null $project_id
77
* @param array $parameters
88
*
99
* @return mixed

lib/Gitlab/Api/IssueLinks.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public function all($project_id, $issue_iid)
1313
}
1414

1515
/**
16-
* @param int $source_project_id
17-
* @param int $source_issue_iid
18-
* @param int $target_project_id
19-
* @param int $target_issue_iid
16+
* @param int|string $source_project_id
17+
* @param int|string $source_issue_iid
18+
* @param int|string $target_project_id
19+
* @param int|string $target_issue_iid
2020
* @return mixed
2121
*/
2222
public function create($source_project_id, $source_issue_iid, $target_project_id, $target_issue_iid)

lib/Gitlab/Api/Issues.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Issues extends AbstractApi
44
{
55
/**
6-
* @param int|null $project_id
6+
* @param int|string|null $project_id
77
* @param array $parameters {
88
*
99
* @var string $state Return all issues or just those that are opened or closed.
@@ -73,7 +73,7 @@ public function update($project_id, $issue_iid, array $params)
7373
/**
7474
* @param int|string $project_id
7575
* @param int $issue_iid
76-
* @param int $to_project_id
76+
* @param int|string $to_project_id
7777
* @return mixed
7878
*/
7979
public function move($project_id, $issue_iid, $to_project_id)

lib/Gitlab/Api/MergeRequests.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ public function show($project_id, $mr_id)
109109
* @param string $source
110110
* @param string $target
111111
* @param string $title
112-
* @param int $assignee @deprecated will be moved into $optionalParams
113-
* @param int $target_project_id @deprecated will be moved into $optionalParams
114-
* @param string $description @deprecated will be moved into $optionalParams
115-
* @param array $optionalParams
112+
* @param int $assignee @deprecated will be moved into $parameters
113+
* @param int|string $target_project_id @deprecated will be moved into $parameters
114+
* @param string $description @deprecated will be moved into $parameters
115+
* @param array<string,mixed> $parameters
116116
* @return mixed
117117
*/
118-
public function create($project_id, $source, $target, $title, $assignee = null, $target_project_id = null, $description = null, array $optionalParams = [])
118+
public function create($project_id, $source, $target, $title, $assignee = null, $target_project_id = null, $description = null, array $parameters = [])
119119
{
120120
$baseParams = [
121121
'source_branch' => $source,
@@ -128,7 +128,7 @@ public function create($project_id, $source, $target, $title, $assignee = null,
128128

129129
return $this->post(
130130
$this->getProjectPath($project_id, 'merge_requests'),
131-
array_merge($baseParams, $optionalParams)
131+
array_merge($baseParams, $parameters)
132132
);
133133
}
134134

lib/Gitlab/Api/Projects.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public function fork($project_id, array $parameters = [])
681681

682682
/**
683683
* @param int|string $project_id
684-
* @param int $forked_project_id
684+
* @param int|string $forked_project_id
685685
* @return mixed
686686
*/
687687
public function createForkRelation($project_id, $forked_project_id)

lib/Gitlab/HttpClient/Message/ResponseMediator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function getPagination(ResponseInterface $response)
4242
return null;
4343
}
4444

45+
/** @var string */
4546
$header = self::getHeader($response, 'Link');
4647
$pagination = array();
4748
foreach (explode(',', $header) as $link) {

lib/Gitlab/HttpClient/Plugin/ApiVersion.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Gitlab\HttpClient\Plugin;
44

55
use Http\Client\Common\Plugin;
6+
use Http\Promise\Promise;
67
use Psr\Http\Message\RequestInterface;
78
use Psr\Http\Message\ResponseInterface;
89

@@ -21,7 +22,12 @@ class ApiVersion implements Plugin
2122
private $redirected = false;
2223

2324
/**
24-
* {@inheritdoc}
25+
* Handle the request and return the response coming from the next callable.
26+
*
27+
* @param RequestInterface $request
28+
* @param callable $next
29+
* @param callable $first
30+
* @return Promise
2531
*/
2632
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
2733
{

lib/Gitlab/HttpClient/Plugin/Authentication.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Gitlab\Client;
66
use Http\Client\Common\Plugin;
7+
use Http\Promise\Promise;
78
use Psr\Http\Message\RequestInterface;
89

910
/**
@@ -44,7 +45,12 @@ public function __construct($method, $token, $sudo = null)
4445
}
4546

4647
/**
47-
* {@inheritdoc}
48+
* Handle the request and return the response coming from the next callable.
49+
*
50+
* @param RequestInterface $request
51+
* @param callable $next
52+
* @param callable $first
53+
* @return Promise
4854
*/
4955
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
5056
{

0 commit comments

Comments
 (0)