aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Profiler/FileProfilerStorage.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Profiler/FileProfilerStorage.php')
-rw-r--r--lib/private/Profiler/FileProfilerStorage.php65
1 files changed, 24 insertions, 41 deletions
diff --git a/lib/private/Profiler/FileProfilerStorage.php b/lib/private/Profiler/FileProfilerStorage.php
index ce09ed51ed9..cd45090e7ca 100644
--- a/lib/private/Profiler/FileProfilerStorage.php
+++ b/lib/private/Profiler/FileProfilerStorage.php
@@ -2,26 +2,8 @@
declare(strict_types = 1);
/**
- * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
- *
- * @author Carl Schwan <carl@carlschwan.eu>
- * @author Alexandre Salomé <alexandre.salome@gmail.com>
- *
- * @license AGPL-3.0-or-later AND MIT
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Profiler;
@@ -45,12 +27,12 @@ class FileProfilerStorage {
public function __construct(string $folder) {
$this->folder = $folder;
- if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
+ if (!is_dir($this->folder) && @mkdir($this->folder, 0777, true) === false && !is_dir($this->folder)) {
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
}
}
- public function find(?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array {
+ public function find(?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null, ?string $statusCode = null): array {
$file = $this->getIndexFilename();
if (!file_exists($file)) {
@@ -64,9 +46,9 @@ class FileProfilerStorage {
while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
$values = str_getcsv($line);
[$csvToken, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
- $csvTime = (int) $csvTime;
+ $csvTime = (int)$csvTime;
- if ($url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) {
+ if ($url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
continue;
}
@@ -99,10 +81,11 @@ class FileProfilerStorage {
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
- if (is_file($file)) {
- unlink($file);
+ $path = $file->getPathname();
+ if (is_file($path)) {
+ unlink($path);
} else {
- rmdir($file);
+ rmdir($path);
}
}
}
@@ -113,7 +96,7 @@ class FileProfilerStorage {
}
if (\function_exists('gzcompress')) {
- $file = 'compress.zlib://'.$file;
+ $file = 'compress.zlib://' . $file;
}
return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
@@ -129,7 +112,7 @@ class FileProfilerStorage {
if (!$profileIndexed) {
// Create directory
$dir = \dirname($file);
- if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
+ if (!is_dir($dir) && @mkdir($dir, 0777, true) === false && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
}
}
@@ -157,11 +140,11 @@ class FileProfilerStorage {
$context = stream_context_create();
if (\function_exists('gzcompress')) {
- $file = 'compress.zlib://'.$file;
+ $file = 'compress.zlib://' . $file;
stream_context_set_option($context, 'zlib', 'level', 3);
}
- if (false === file_put_contents($file, serialize($data), 0, $context)) {
+ if (file_put_contents($file, serialize($data), 0, $context) === false) {
return false;
}
@@ -195,7 +178,7 @@ class FileProfilerStorage {
$folderA = substr($token, -2, 2);
$folderB = substr($token, -4, 2);
- return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
+ return $this->folder . '/' . $folderA . '/' . $folderB . '/' . $token;
}
/**
@@ -204,7 +187,7 @@ class FileProfilerStorage {
* @return string The index filename
*/
protected function getIndexFilename(): string {
- return $this->folder.'/index.csv';
+ return $this->folder . '/index.csv';
}
/**
@@ -220,7 +203,7 @@ class FileProfilerStorage {
$line = '';
$position = ftell($file);
- if (0 === $position) {
+ if ($position === 0) {
return null;
}
@@ -229,7 +212,7 @@ class FileProfilerStorage {
$position -= $chunkSize;
fseek($file, $position);
- if (0 === $chunkSize) {
+ if ($chunkSize === 0) {
// bof reached
break;
}
@@ -237,23 +220,23 @@ class FileProfilerStorage {
$buffer = fread($file, $chunkSize);
if (false === ($upTo = strrpos($buffer, "\n"))) {
- $line = $buffer.$line;
+ $line = $buffer . $line;
continue;
}
$position += $upTo;
- $line = substr($buffer, $upTo + 1).$line;
+ $line = substr($buffer, $upTo + 1) . $line;
fseek($file, max(0, $position), \SEEK_SET);
- if ('' !== $line) {
+ if ($line !== '') {
break;
}
}
- return '' === $line ? null : $line;
+ return $line === '' ? null : $line;
}
- protected function createProfileFromData(string $token, array $data, IProfile $parent = null): IProfile {
+ protected function createProfileFromData(string $token, array $data, ?IProfile $parent = null): IProfile {
$profile = new Profile($token);
$profile->setMethod($data['method']);
$profile->setUrl($data['url']);
@@ -275,7 +258,7 @@ class FileProfilerStorage {
}
if (\function_exists('gzcompress')) {
- $file = 'compress.zlib://'.$file;
+ $file = 'compress.zlib://' . $file;
}
$profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));