Skip to content

Commit

Permalink
Remove duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Feb 17, 2024
1 parent 6705bbd commit 4bc1d94
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 93 deletions.
1 change: 1 addition & 0 deletions Src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ function loadConfig()

require_once "lib/functions.php";
require_once "lib/database.php";
require_once "lib/request.php";
require_once "lib/appveyor.php";
require_once "lib/github.php";
51 changes: 2 additions & 49 deletions Src/lib/appveyor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,7 @@ function requestAppVeyor($url, $data = null)
global $appVeyorKey;

$baseUrl = "https://ci.appveyor.com/api/";
$url = $baseUrl . $url;

$headers = array();
$headers[] = "User-Agent: " . USER_AGENT;
$headers[] = "Content-type: application/json";
$headers[] = "Authorization: Bearer " . $appVeyorKey;

$fields = array(
CURLOPT_URL => $baseUrl . $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $headers
);

if ($data !== null) {
$fields[CURLOPT_POST] = true;
$fields[CURLOPT_POSTFIELDS] = json_encode($data);
}

$curl = curl_init();

curl_setopt_array($curl, $fields);

$response = curl_exec($curl);

if ($response === false) {
echo htmlspecialchars($url);
echo "\r\n";
die(curl_error($curl));
}

if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
echo htmlspecialchars($url);
echo "\r\n";
die($response);
}

$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$headers = extractHeaders($header);
$body = substr($response, $headerSize);
curl_close($curl);

return array("headers" => $headers, "body" => $body);
return doRequest($url, $appVeyorKey, $data);
}
46 changes: 2 additions & 44 deletions Src/lib/github.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,9 @@
function requestGitHub($gitHubToken, $url, $data = null)
{
$baseUrl = "https://api.github.com/";
$url = $baseUrl . $url;

Check notice on line 12 in Src/lib/github.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/github.php#L12

Concat operator must not be surrounded by spaces

$headers = array();
$headers[] = "User-Agent: " . USER_AGENT;
$headers[] = "Content-type: application/json";
$headers[] = "Authorization: Bearer " . $gitHubToken;

$fields = array(
CURLOPT_URL => $baseUrl . $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $headers
);

if ($data !== null) {
$fields[CURLOPT_POST] = true;
$fields[CURLOPT_POSTFIELDS] = json_encode($data);
}

$curl = curl_init();

curl_setopt_array($curl, $fields);

$response = curl_exec($curl);

if ($response === false) {
echo htmlspecialchars($url);
echo "\r\n";
die(curl_error($curl));
}

$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$headers = extractHeaders($header);
$body = substr($response, $headerSize);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

return array("status" => $http_code, "headers" => $headers, "body" => $body);
return doRequest($url, $gitHubToken, $data);
}

function generateAppToken()
Expand Down
48 changes: 48 additions & 0 deletions Src/lib/request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

Check notice on line 1 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L1

Whitespace found at end of line

function doRequest($url, $authorizationBearerToken, $data = null){

Check notice on line 3 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L3

Avoid excessively long variable names like $authorizationBearerToken. Keep variable name length under 20.

Check notice on line 3 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L3

Expected 2 blank lines before function; 1 found

Check notice on line 3 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L3

Incorrect spacing between argument "$data" and equals sign; expected 0 but found 1

Check notice on line 3 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L3

Opening brace should be on a new line
$headers = array();

Check notice on line 4 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L4

Short array syntax must be used to define arrays
$headers[] = "User-Agent: " . USER_AGENT;
$headers[] = "Content-type: application/json";
$headers[] = "Authorization: Bearer " . $authorizationBearerToken;

Check notice on line 7 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L7

Concat operator must not be surrounded by spaces

$fields = array(

Check notice on line 9 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L9

Short array syntax must be used to define arrays
CURLOPT_URL => $url,

Check notice on line 10 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L10

Array key not aligned correctly; expected 15 spaces but found 8
CURLOPT_RETURNTRANSFER => true,

Check notice on line 11 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L11

Array key not aligned correctly; expected 15 spaces but found 8
CURLOPT_HEADER => true,

Check notice on line 12 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L12

Array key not aligned correctly; expected 15 spaces but found 8
CURLOPT_ENCODING => "",

Check notice on line 13 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L13

Array key not aligned correctly; expected 15 spaces but found 8
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,

Check notice on line 16 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L16

Array key not aligned correctly; expected 15 spaces but found 8
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $headers

Check notice on line 20 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L20

Array key not aligned correctly; expected 15 spaces but found 8
);

if ($data !== null) {
$fields[CURLOPT_POST] = true;
$fields[CURLOPT_POSTFIELDS] = json_encode($data);
}

$curl = curl_init();

Check warning on line 28 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L28

The use of function curl_init() is discouraged

curl_setopt_array($curl, $fields);

$response = curl_exec($curl);

if ($response === false) {
echo htmlspecialchars($url);

Check failure on line 35 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L35

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found 'htmlspecialchars'.
echo "\r\n";

Check failure on line 36 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L36

Use of echo language construct is discouraged.
die(curl_error($curl));

Check warning on line 37 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L37

The function doRequest() contains an exit expression.
}

$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);

Check warning on line 40 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L40

The use of function curl_getinfo() is discouraged
$header = substr($response, 0, $headerSize);
$headers = extractHeaders($header);
$body = substr($response, $headerSize);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

Check warning on line 44 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L44

The use of function curl_getinfo() is discouraged
curl_close($curl);

Check warning on line 45 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L45

The use of function curl_close() is discouraged

return array("status" => $http_code, "headers" => $headers, "body" => $body);

Check notice on line 47 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L47

Array with multiple values cannot be declared on a single line

Check notice on line 47 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L47

Short array syntax must be used to define arrays
}

Check notice on line 48 in Src/lib/request.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/lib/request.php#L48

Expected 1 blank line before closing function brace; 0 found

0 comments on commit 4bc1d94

Please sign in to comment.