Skip to content

Commit

Permalink
chore: move adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Sep 19, 2023
1 parent 98e0eeb commit 07aa0f0
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 115 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
"ext-gd": "*",
"ext-pdo": "*",
"ext-libxml": "*",
"ext-simplexml": "*"
"ext-simplexml": "*",
"ext-fileinfo": "*",
"ext-exif": "*"
},
"extra": {
"bamarni-bin": {
Expand Down
24 changes: 10 additions & 14 deletions lib/private/Image/BmpToResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class BmpToResource {
/** @var string[][] $pixelArray */
private $pixelArray;

/** @var resource $resource */
private $resource;
private \GdImage $resource;

/** @var array $bytesPerDepth */
private $bytesPerDepth = [
Expand Down Expand Up @@ -87,10 +86,11 @@ public function toResource() {
$this->pixelArray = $this->readPixelArray();

// create gd image
$this->resource = \imagecreatetruecolor($this->header['width'], $this->header['height']);
if ($this->resource === false) {
$resource = \imagecreatetruecolor($this->header['width'], $this->header['height']);
if ($resource === false) {
throw new \RuntimeException('imagecreatetruecolor failed for file ' . $this->getFilename() . '" with dimensions ' . $this->header['width'] . 'x' . $this->header['height']);
}
$this->resource = $resource;

$this->pixelArrayToImage();
} catch (\Exception $e) {
Expand Down Expand Up @@ -149,7 +149,7 @@ private function readDibHeader() {
}

$validBitDepth = \array_keys($this->bytesPerDepth);
if (!\in_array($dibHeader['bits'], $validBitDepth)) {
if (!\in_array($dibHeader['bits'], $validBitDepth, true)) {
throw new \UnexpectedValueException('Bit Depth ' . $dibHeader['bits'] . ' in ' . $this->getFilename() . ' is not supported');
}

Expand All @@ -159,7 +159,7 @@ private function readDibHeader() {
private function fixImageSize($header) {
// No compression - calculate it in our own
if ($header['compression'] === self::COMPRESSION_BI_RGB) {
$bytesPerRow = \intval(\floor(($header['bits'] * $header['width'] + 31) / 32) * 4);
$bytesPerRow = (int)(\floor(($header['bits'] * $header['width'] + 31) / 32) * 4);
$imageSize = $bytesPerRow * \abs($header['height']);
} else {
$imageSize = $this->file->getSize() - $this->header['offset'];
Expand Down Expand Up @@ -196,7 +196,7 @@ private function readPixelArray() {
$this->file->fseek($this->header['offset'], SEEK_SET);
$pixelString = $this->readFile($this->header['imagesize']);

$bytesPerRow = \intval(\floor(($this->header['bits'] * $this->header['width'] + 31) / 32) * 4);
$bytesPerRow = (int)(\floor(($this->header['bits'] * $this->header['width'] + 31) / 32) * 4);
$plainPixelArray = \str_split($pixelString, $bytesPerRow);

// Positive height: Bottom row first.
Expand All @@ -212,10 +212,7 @@ private function readPixelArray() {
return $pixelArray;
}

/**
* @return resource
*/
private function pixelArrayToImage() {
private function pixelArrayToImage(): \GdImage {
$x = 0;
$y = 0;
foreach ($this->pixelArray as $pixelRow) {
Expand Down Expand Up @@ -246,7 +243,7 @@ private function pixelArrayToImage() {
private function getColors($raw) {
$extra = \chr(0); // used to complement an argument to word or double word
$colors = [];
if (\in_array($this->header['bits'], [32, 24])) {
if (\in_array($this->header['bits'], [32, 24], true)) {
$colors = @\unpack('V', $raw . $extra);
} elseif ($this->header['bits'] === 16) {
$colors = @\unpack('v', $raw);
Expand All @@ -264,8 +261,7 @@ function ($i) {
);
}

$colors = \array_values($colors);
return $colors;
return \array_values($colors);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/IntegrityCheck/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ public function getResults() {
return \json_decode($cachedResults, true);
}

return \json_decode($this->getAppValue(self::CACHE_KEY, '{}'), true);
$v = $this->getAppValue(self::CACHE_KEY, '{}') ?? '{}';
return \json_decode($v, true);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/private/Memcache/NullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

namespace OC\Memcache;

class NullCache extends Cache implements \OCP\IMemcache {
public function get($key) {
use OCP\IMemcache;

class NullCache extends Cache implements IMemcache {
public function get($key): mixed {
return null;
}

public function set($key, $value, $ttl = 0) {
public function set($key, $value, $ttl = 0): mixed {
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions lib/private/Memcache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ protected function getNameSpace() {
return $this->prefix;
}

public function get($key) {
public function get($key): mixed {
$result = self::$cache->get($this->getNameSpace() . $key);
if ($result === false && !self::$cache->exists($this->getNameSpace() . $key)) {
return null;
} else {
return \json_decode($result, true);
}

return \json_decode($result, true);
}

public function set($key, $value, $ttl = 0) {
public function set($key, $value, $ttl = 0): mixed {
if ($ttl > 0) {
return self::$cache->setex($this->getNameSpace() . $key, $ttl, \json_encode($value));
} else {
return self::$cache->set($this->getNameSpace() . $key, \json_encode($value));
}

return self::$cache->set($this->getNameSpace() . $key, \json_encode($value));
}

public function hasKey($key) {
Expand Down
79 changes: 41 additions & 38 deletions lib/private/legacy/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ public function __construct($imageRef = null, $logger = null, $config = null) {

/**
* Determine whether the object contains an image resource.
*
* @return bool
*/
public function valid() { // apparently you can't name a method 'empty'...
return \is_resource($this->resource);
public function valid(): bool {
// apparently you can't name a method 'empty'...
return $this->resource instanceof \GdImage;
}

/**
Expand Down Expand Up @@ -217,7 +216,9 @@ public function save($filePath = null, $mimeType = null) {
if ($filePath === null && $this->filePath === null) {
$this->logger->error(__METHOD__ . '(): called with no path.', ['app' => 'core']);
return false;
} elseif ($filePath === null && $this->filePath !== null) {
}

if ($filePath === null && $this->filePath !== null) {
$filePath = $this->filePath;
}
return $this->_output($filePath, $mimeType);
Expand All @@ -239,7 +240,9 @@ private function _output($filePath = null, $mimeType = null) {
if (!\is_writable(\dirname($filePath))) {
$this->logger->error(__METHOD__ . '(): Directory \'' . \dirname($filePath) . '\' is not writable.', ['app' => 'core']);
return false;
} elseif (\is_writable(\dirname($filePath)) && \file_exists($filePath) && !\is_writable($filePath)) {
}

if (\is_writable(\dirname($filePath)) && \file_exists($filePath) && !\is_writable($filePath)) {
$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', ['app' => 'core']);
return false;
}
Expand Down Expand Up @@ -315,10 +318,7 @@ public function __invoke() {
return $this->show();
}

/**
* @return resource Returns the image resource in any.
*/
public function resource() {
public function resource(): ?GdImage {
return $this->resource;
}

Expand Down Expand Up @@ -490,18 +490,18 @@ public function fixOrientation() {
\imagedestroy($this->resource);
$this->resource = $res;
return true;
} else {
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', ['app' => 'core']);
return false;
}
} else {
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', ['app' => 'core']);

$this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', ['app' => 'core']);
return false;
}
} else {
$this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', ['app' => 'core']);

$this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', ['app' => 'core']);
return false;
}

$this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', ['app' => 'core']);
return false;
}
return false;
}
Expand Down Expand Up @@ -541,9 +541,9 @@ public function load(\GdImage|string $imageRef) {
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
*
* @param resource $handle
* @return resource|false An image resource or false on error
* @return GdImage|false|null An image resource or false on error
*/
public function loadFromFileHandle($handle) {
public function loadFromFileHandle($handle): GdImage|false|null {
$contents = \stream_get_contents($handle);
if ($this->loadFromData($contents)) {
$this->adjustStreamChunkSize($handle);
Expand Down Expand Up @@ -671,7 +671,10 @@ public function loadFromFile($imagePath = false) {
}
break;
case IMAGETYPE_BMP:
$this->resource = $this->imagecreatefrombmp($imagePath);
$resource = $this->imagecreatefrombmp($imagePath);
if ($resource !== false) {
$this->resource = $resource;
}
break;
/*
case IMAGETYPE_TIFF_II: // (intel byte order)
Expand Down Expand Up @@ -718,52 +721,52 @@ public function loadFromFile($imagePath = false) {
* Loads an image from a string of data.
*
* @param string $str A string of image data as read from a file.
* @return bool|resource An image resource or false on error
* @return bool An image resource or false on error
*/
public function loadFromData($str) {
if (\is_resource($str)) {
if (!\is_string($str)) {
return false;
}
$this->resource = @\imagecreatefromstring($str);
$resource = @\imagecreatefromstring($str);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($str);
}
if (\is_resource($this->resource)) {
\imagealphablending($this->resource, false);
\imagesavealpha($this->resource, true);
}

if (!$this->resource) {
if (!$resource) {
$this->logger->debug('OC_Image->loadFromFile, could not load', ['app' => 'core']);
return false;
}
return $this->resource;
$this->resource = $resource;
\imagealphablending($this->resource, false);
\imagesavealpha($this->resource, true);

return true;
}

/**
* Loads an image from a base64 encoded string.
*
* @param string $str A string base64 encoded string of image data.
* @return bool|resource An image resource or false on error
* @return bool An image resource or false on error
*/
public function loadFromBase64($str) {
private function loadFromBase64($str) {
if (!\is_string($str)) {
return false;
}
$data = \base64_decode($str);
if ($data) { // try to load from string data
$this->resource = @\imagecreatefromstring($data);
$resource = @\imagecreatefromstring($data);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($data);
}
if (!$this->resource) {
if ($resource === false) {
$this->logger->debug('OC_Image->loadFromBase64, could not load', ['app' => 'core']);
return false;
}
return $this->resource;
} else {
return false;
$this->resource = $resource;
return true;
}

return false;
}

/**
Expand Down Expand Up @@ -1019,7 +1022,7 @@ public function __destruct() {
* @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
* @author mgutt <marc@gutt.it>
* @version 1.00
* @param resource $im
* @param \GdImage $im
* @param string $fileName [optional] <p>The path to save the file to.</p>
* @param int $bit [optional] <p>Bit depth, (default is 24).</p>
* @param int $compression [optional]
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/AppFramework/Db/MapperTestUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ protected function setUp(): void {
$this->query = $this->createMock('\PDOStatement');
$this->iterators = [];
$this->fetchAt = 0;

$this->query->method('execute')->willReturn(true);
}

/**
Expand Down

0 comments on commit 07aa0f0

Please sign in to comment.