Skip to content

Commit

Permalink
Merge pull request #620 from catalyst/fix-codestandards
Browse files Browse the repository at this point in the history
Fix code standards and deprecations
  • Loading branch information
brendanheywood authored Aug 1, 2024
2 parents 5284cc9 + e8b7db7 commit 4e04360
Show file tree
Hide file tree
Showing 34 changed files with 223 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function get_candidates_sql_params() {
return [
'consistancythreshold' => $consistancythreshold,
'location' => OBJECT_LOCATION_DUPLICATED,
'sizethreshold' => $this->config->sizethreshold
'sizethreshold' => $this->config->sizethreshold,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function get_candidates_sql() {
*/
public function get_candidates_sql_params() {
return [
'location' => OBJECT_LOCATION_ORPHANED
'location' => OBJECT_LOCATION_ORPHANED,
];
}
}
2 changes: 1 addition & 1 deletion classes/local/object_manipulator/manipulator_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class manipulator_builder {
pusher::class,
recoverer::class,
checker::class,
orphaner::class
orphaner::class,
];

/** @var string $manipulatorclass */
Expand Down
6 changes: 3 additions & 3 deletions classes/local/report/location_report_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function build_report($reportid) {
OBJECT_LOCATION_DUPLICATED,
OBJECT_LOCATION_EXTERNAL,
OBJECT_LOCATION_ORPHANED,
OBJECT_LOCATION_ERROR
OBJECT_LOCATION_ERROR,
];

$totalcount = 0;
Expand All @@ -71,7 +71,7 @@ public function build_report($reportid) {

if ($location !== OBJECT_LOCATION_ORPHANED) {
// Process the query normally.
$result = $DB->get_record_sql($sql, array($location));
$result = $DB->get_record_sql($sql, [$location]);
} else if ($location === OBJECT_LOCATION_ORPHANED) {
// Start the query from objectfs, for ORPHANED objects, they are not located in the files table.
$sql = 'SELECT COALESCE(count(sub.contenthash) ,0) AS objectcount
Expand All @@ -80,7 +80,7 @@ public function build_report($reportid) {
LEFT JOIN {files} f on f.contenthash = o.contenthash
GROUP BY o.contenthash, f.filesize, o.location
HAVING o.location = ?' . $localsql .') AS sub';
$result = $DB->get_record_sql($sql, array($location));
$result = $DB->get_record_sql($sql, [$location]);
$result->objectsum = 0;
}

Expand Down
4 changes: 2 additions & 2 deletions classes/local/report/object_location_history_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public function __construct() {
public function query_db($pagesize, $useinitialsbar = true) {
global $DB;
$fields = 'CONCAT(reportid, datakey) AS uid, datakey AS location, objectcount AS count, objectsum AS size';
$conditions = array('reporttype' => 'location');
$conditions = ['reporttype' => 'location'];
$rawrecords = $DB->get_records('tool_objectfs_report_data', $conditions, 'reportid', $fields);
$reports = objectfs_report::get_report_ids();

// Used to fallback to when the expected record is not there.
// NOTE: This avoids the need to null coalesce on a non-existing count/size.
$emptyrecord = (object)[
'count' => 0,
'size' => 0
'size' => 0,
];
foreach ($reports as $id => $timecreated) {
// Initialises the records to be used, and fallback to an empty one if not found.
Expand Down
4 changes: 2 additions & 2 deletions classes/local/report/object_status_history_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function query_db($pagesize, $useinitialsbar = true) {
default:
$sort = 'heading ASC';
}
$params = array('reporttype' => $this->reporttype, 'reportid' => $this->reportid);
$params = ['reporttype' => $this->reporttype, 'reportid' => $this->reportid];
$fields = 'datakey AS heading, objectcount AS count, objectsum AS size';
$rows = $DB->get_records('tool_objectfs_report_data', $params, $sort, $fields);
$this->rawdata = $rows;
Expand Down Expand Up @@ -196,7 +196,7 @@ public function add_barchart($value, $max, $type, $precision = 0) {
if ($max > 0) {
$share = round(100 * $value / $max, $precision);
}
$htmlparams = array('class' => 'ofs-bar', 'style' => 'width:'.$share.'%');
$htmlparams = ['class' => 'ofs-bar', 'style' => 'width:'.$share.'%'];

switch ($type) {
case 'count':
Expand Down
4 changes: 2 additions & 2 deletions classes/local/report/objectfs_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function generate_status_report() {
public static function cleanup_reports() {
global $DB;
$reportdate = time() - YEARSECS;
$params = array('reportdate' => $reportdate);
$params = ['reportdate' => $reportdate];
$reports = $DB->get_records_select('tool_objectfs_reports', 'reportdate < :reportdate', $params, 'id', 'id');
$reportids = array_keys($reports);
$DB->delete_records_list('tool_objectfs_reports', 'id', $reportids);
Expand All @@ -177,7 +177,7 @@ public static function get_report_types() {
*/
public static function get_report_ids() {
global $DB;
$reports = array();
$reports = [];
$records = $DB->get_records('tool_objectfs_reports', null, 'id DESC', 'id, reportdate');
foreach ($records as $record) {
$reports[$record->id] = $record->reportdate;
Expand Down
12 changes: 6 additions & 6 deletions classes/local/store/azure/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ public function get_relative_path_from_fullpath($fullpath) {
* @return resource
*/
public function get_seekable_stream_context() {
$context = stream_context_create(array(
'blob' => array(
'seekable' => true
)
));
$context = stream_context_create([
'blob' => [
'seekable' => true,
],
]);
return $context;
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public function test_connection() {
public function test_permissions($testdelete) {
$permissions = new \stdClass();
$permissions->success = true;
$permissions->messages = array();
$permissions->messages = [];

try {
$result = $this->client->createBlockBlob($this->container, 'permissions_check_file', 'permissions_check_file');
Expand Down
12 changes: 6 additions & 6 deletions classes/local/store/digitalocean/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public function set_client($config) {
return;
}

$this->client = \Aws\S3\S3Client::factory(array(
'credentials' => array('key' => $config->do_key, 'secret' => $config->do_secret),
$this->client = \Aws\S3\S3Client::factory([
'credentials' => ['key' => $config->do_key, 'secret' => $config->do_secret],
'region' => $config->do_region,
'endpoint' => 'https://' . $config->do_region . '.digitaloceanspaces.com',
'version' => AWS_API_VERSION
));
'version' => AWS_API_VERSION,
]);
}

/**
Expand All @@ -93,13 +93,13 @@ public function set_client($config) {
*/
public function define_client_section($settings, $config) {

$regionoptions = array(
$regionoptions = [
'sfo2' => 'sfo2 (San Fransisco)',
'nyc3' => 'nyc3 (New York City)',
'ams3' => 'ams3 (Amsterdam)',
'sgp1' => 'spg1 (Singapore)',
'fra1' => 'fra1 (Frankfurt)',
);
];

$settings->add(new \admin_setting_heading('tool_objectfs/do',
new \lang_string('settings:do:header', 'tool_objectfs'), ''));
Expand Down
2 changes: 1 addition & 1 deletion classes/local/store/object_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function verify_object($contenthash, $localpath);
*
* @return mixed
*/
public function generate_presigned_url($contenthash, $headers = array());
public function generate_presigned_url($contenthash, $headers = []);

/**
* support_presigned_urls
Expand Down
2 changes: 1 addition & 1 deletion classes/local/store/object_client_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function support_presigned_urls() {
*
* @throws \coding_exception
*/
public function generate_presigned_url($contenthash, $headers = array()) {
public function generate_presigned_url($contenthash, $headers = []) {
throw new \coding_exception("Pre-signed URLs not supported");
}

Expand Down
6 changes: 3 additions & 3 deletions classes/local/store/object_file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public function delete_empty_dirs($rootpath = '') {
$pathinfo['filename'],
$pathinfo['basename'],
$pathinfo['filename'],
$pathinfo['basename']
$pathinfo['basename'],
]);

if (!$exists) {
Expand Down Expand Up @@ -844,7 +844,7 @@ protected function copy_from_external_to_local($contenthash) {
* @return bool
* @throws \dml_exception
*/
public function redirect_to_presigned_url($contenthash, $headers = array()) {
public function redirect_to_presigned_url($contenthash, $headers = []) {
global $FULLME;
try {
$signedurl = $this->externalclient->generate_presigned_url($contenthash, $headers);
Expand Down Expand Up @@ -926,7 +926,7 @@ public function presigned_url_should_redirect_file($file) {
* @return bool
* @throws \dml_exception
*/
public function presigned_url_should_redirect($contenthash, $headers = array()) {
public function presigned_url_should_redirect($contenthash, $headers = []) {
// Redirect regardless.
if ($this->externalclient->presignedminfilesize == 0 &&
manager::all_extensions_whitelisted()) {
Expand Down
68 changes: 37 additions & 31 deletions classes/local/store/s3/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __construct($config) {
* @return array
*/
public function __sleep() {
return array('bucket');
return ['bucket'];
}

/**
Expand Down Expand Up @@ -153,17 +153,17 @@ public function set_client($config) {
return;
}

$options = array(
$options = [
'region' => $config->s3_region,
'version' => AWS_API_VERSION
);
'version' => AWS_API_VERSION,
];

if (empty($config->s3_usesdkcreds)) {
$options['credentials'] = array('key' => $config->s3_key, 'secret' => $config->s3_secret);
$options['credentials'] = ['key' => $config->s3_key, 'secret' => $config->s3_secret];
}

if ($config->useproxy) {
$options['http'] = array('proxy' => $this->get_proxy_string());
$options['http'] = ['proxy' => $this->get_proxy_string()];
}

// Support base_url config for aws api compatible endpoints.
Expand Down Expand Up @@ -199,9 +199,9 @@ private function get_md5_from_hash($contenthash) {

try {
$key = $this->get_filepath_from_hash($contenthash);
$result = $this->client->headObject(array(
$result = $this->client->headObject([
'Bucket' => $this->bucket,
'Key' => $this->bucketkeyprefix . $key));
'Key' => $this->bucketkeyprefix . $key]);
} catch (\Aws\S3\Exception\S3Exception $e) {
return false;
}
Expand Down Expand Up @@ -268,11 +268,11 @@ public function rename_file($currentpath, $destinationpath) {
* @return mixed
*/
public function get_seekable_stream_context() {
$context = stream_context_create(array(
's3' => array(
'seekable' => true
)
));
$context = stream_context_create([
's3' => [
'seekable' => true,
],
]);
return $context;
}

Expand Down Expand Up @@ -306,7 +306,7 @@ public function test_connection() {
$connection->success = false;
$connection->details = get_string('settings:notconfigured', 'tool_objectfs');
} else {
$this->client->headBucket(array('Bucket' => $this->bucket));
$this->client->headBucket(['Bucket' => $this->bucket]);
}
} catch (\Aws\S3\Exception\S3Exception $e) {
$connection->success = false;
Expand Down Expand Up @@ -334,29 +334,29 @@ public function test_connection() {
public function test_permissions($testdelete) {
$permissions = new \stdClass();
$permissions->success = true;
$permissions->messages = array();
$permissions->messages = [];

if ($this->is_functional()) {
$permissions->success = false;
$permissions->messages = array();
$permissions->messages = [];
return $permissions;
}

try {
$result = $this->client->putObject(array(
$result = $this->client->putObject([
'Bucket' => $this->bucket,
'Key' => $this->bucketkeyprefix . 'permissions_check_file',
'Body' => 'test content'));
'Body' => 'test content']);
} catch (\Aws\S3\Exception\S3Exception $e) {
$details = $this->get_exception_details($e);
$permissions->messages[get_string('settings:writefailure', 'tool_objectfs') . $details] = 'notifyproblem';
$permissions->success = false;
}

try {
$result = $this->client->getObject(array(
$result = $this->client->getObject([
'Bucket' => $this->bucket,
'Key' => $this->bucketkeyprefix . 'permissions_check_file'));
'Key' => $this->bucketkeyprefix . 'permissions_check_file']);
} catch (\Aws\S3\Exception\S3Exception $e) {
$errorcode = $e->getAwsErrorCode();
// Write could have failed.
Expand All @@ -371,7 +371,7 @@ public function test_permissions($testdelete) {
try {
$result = $this->client->deleteObject([
'Bucket' => $this->bucket,
'Key' => $this->bucketkeyprefix . 'permissions_check_file'
'Key' => $this->bucketkeyprefix . 'permissions_check_file',
]);
$permissions->messages[get_string('settings:deletesuccess', 'tool_objectfs')] = 'warning';
$permissions->success = false;
Expand Down Expand Up @@ -539,7 +539,7 @@ public function support_presigned_urls() {
* @return signed_url
* @throws \Exception
*/
public function generate_presigned_url($contenthash, $headers = array()) {
public function generate_presigned_url($contenthash, $headers = []) {
if ('cf' === $this->signingmethod) {
return $this->generate_presigned_url_cloudfront($contenthash, $headers);
}
Expand Down Expand Up @@ -670,7 +670,13 @@ private function get_nice_filename($headers) {

if (!empty($originalfilename)) {
$result['Content-Disposition'] = $contentdisposition;
$result['filename'] = 'filename="' . utf8_encode($originalfilename) . '"';
// The filename parameter must be in ISO-8859-1, however it works in browsers if
// you treat the original UTF-8 string as ISO-8859-1 characters. To achieve that
// here, we encode the UTF-8 as if it were ISO-8859-1. This behaviour is hideous
// so it would be nice to use the optional filename* field (RFC 5987) but S3 still
// complains if we do that.
$jankyfilename = \core_text::convert($originalfilename, 'ISO-8859-1');
$result['filename'] = 'filename="' . $jankyfilename . '"';
$result['Content-Type'] = $originalcontenttype;
}
}
Expand Down Expand Up @@ -828,18 +834,18 @@ public function curl_range_request_to_presigned_url($contenthash, $ranges, $head
} catch (\Exception $e) {
throw new \coding_exception('Failed to generate pre-signed url: ' . $e->getMessage());
}
$headers = array(
$headers = [
'Range: bytes=' . $ranges->rangefrom . '-' . $ranges->rangeto,
);
];
$curl = new \curl();
$curl->setopt(array('CURLOPT_HTTP_VERSION' => CURL_HTTP_VERSION_1_1));
$curl->setopt(array('CURLOPT_RETURNTRANSFER' => true));
$curl->setopt(array('CURLOPT_SSL_VERIFYPEER' => false));
$curl->setopt(array('CURLOPT_CONNECTTIMEOUT' => 15));
$curl->setopt(array('CURLOPT_TIMEOUT' => 15));
$curl->setopt(['CURLOPT_HTTP_VERSION' => CURL_HTTP_VERSION_1_1]);
$curl->setopt(['CURLOPT_RETURNTRANSFER' => true]);
$curl->setopt(['CURLOPT_SSL_VERIFYPEER' => false]);
$curl->setopt(['CURLOPT_CONNECTTIMEOUT' => 15]);
$curl->setopt(['CURLOPT_TIMEOUT' => 15]);
$curl->setHeader($headers);
$content = $curl->get($url);
return array('responseheaders' => $curl->getResponse(), 'content' => $content, 'url' => $url);
return ['responseheaders' => $curl->getResponse(), 'content' => $content, 'url' => $url];
}

/**
Expand Down
Loading

0 comments on commit 4e04360

Please sign in to comment.