Skip to content

Commit

Permalink
refactor: migrate Gaufrette\File to php 8.0 (#709)
Browse files Browse the repository at this point in the history
* refactor: migrate Gaufrette\File to php 8.0

* Update src/Gaufrette/File.php

Co-authored-by: Kévin <[email protected]>

---------

Co-authored-by: Kévin <[email protected]>
  • Loading branch information
PedroTroller and KevinArtus authored Dec 7, 2023
1 parent add9aa6 commit 568a0c4
Showing 1 changed file with 34 additions and 78 deletions.
112 changes: 34 additions & 78 deletions src/Gaufrette/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,43 @@
*/
class File
{
protected $key;
protected $filesystem;

/**
* Content variable is lazy. It will not be read from filesystem until it's requested first time.
*
* @var mixed content
*/
protected $content = null;
protected mixed $content = null;

/**
* @var array metadata in associative array. Only for adapters that support metadata
* Metadata in associative array. Only for adapters that support metadata
*/
protected $metadata = null;
protected ?array $metadata = null;

/**
* Human readable filename (usually the end of the key).
*
* @var string name
*/
protected $name = null;
protected string $name;

/**
* File size in bytes.
*
* @var int size
*/
protected $size = 0;
protected int $size = 0;

/**
* File date modified.
*
* @var int mtime
*/
protected $mtime = null;
protected ?int $mtime = null;

/**
* @param string $key
* @param FilesystemInterface $filesystem
* @param FilesystemInterface&Filesystem $filesystem
*/
public function __construct($key, FilesystemInterface $filesystem)
public function __construct(private string $key, private FilesystemInterface $filesystem)
{
$this->key = $key;
$this->name = $key;
$this->filesystem = $filesystem;
}

/**
* Returns the key.
*
* @return string
*/
public function getKey()
public function getKey(): string
{
return $this->key;
}
Expand All @@ -75,10 +59,8 @@ public function getKey()
* @throws FileNotFound
*
* @param array $metadata optional metadata which should be set when read
*
* @return string
*/
public function getContent($metadata = [])
public function getContent(array $metadata = []): string
{
if (isset($this->content)) {
return $this->content;
Expand All @@ -88,18 +70,14 @@ public function getContent($metadata = [])
return $this->content = $this->filesystem->read($this->key);
}

/**
* @return string name of the file
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @return int size of the file
*/
public function getSize()
public function getSize(): int
{
if ($this->size) {
return $this->size;
Expand All @@ -108,39 +86,30 @@ public function getSize()
try {
return $this->size = $this->filesystem->size($this->getKey());
} catch (FileNotFound $exception) {
return 0;
}

return 0;
}

/**
* Returns the file modified time.
*
* @return int
* @return int|false Returns the file modified time.
*/
public function getMtime()
public function getMtime(): int|bool
{
return $this->mtime = $this->filesystem->mtime($this->key);
}

/**
* @param int $size size of the file
*/
public function setSize($size)
public function setSize(int $size): void
{
$this->size = $size;
}

/**
* Sets the content.
*
* @param string $content
* @param array $metadata optional metadata which should be send when write
* @param array $metadata optional metadata which should be send when write
*
* @return int The number of bytes that were written into the file, or
* @return int|bool The number of bytes that were written into the file, or
* FALSE on failure
*/
public function setContent($content, $metadata = [])
public function setContent(string $content, array $metadata = []): int|bool
{
$this->content = $content;
$this->setMetadata($metadata);
Expand All @@ -149,19 +118,16 @@ public function setContent($content, $metadata = [])
}

/**
* @param string $name name of the file
*/
public function setName($name)
public function setName(string $name): void
{
$this->name = $name;
}

/**
* Indicates whether the file exists in the filesystem.
*
* @return bool
*/
public function exists()
public function exists(): bool
{
return $this->filesystem->has($this->key);
}
Expand All @@ -176,7 +142,7 @@ public function exists()
*
* @return bool TRUE on success
*/
public function delete($metadata = [])
public function delete(array $metadata = []): bool
{
$this->setMetadata($metadata);

Expand All @@ -185,20 +151,16 @@ public function delete($metadata = [])

/**
* Creates a new file stream instance of the file.
*
* @return Stream
*/
public function createStream()
public function createStream(): Stream
{
return $this->filesystem->createStream($this->key);
}

/**
* Rename the file and move it to its new location.
*
* @param string $newKey
*/
public function rename($newKey)
public function rename(string $newKey): void
{
$this->filesystem->rename($this->key, $newKey);

Expand All @@ -207,27 +169,21 @@ public function rename($newKey)

/**
* Sets the metadata array to be stored in adapters that can support it.
*
* @param array $metadata
*
* @return bool
*/
protected function setMetadata(array $metadata)
protected function setMetadata(array $metadata): bool
{
if ($metadata && $this->supportsMetadata()) {
$this->filesystem->getAdapter()->setMetadata($this->key, $metadata);
if ([] === $metadata) {
return false;
}

$adapter = $this->filesystem->getAdapter();

return true;
if (false === $adapter instanceof MetadataSupporter) {
return false;
}

return false;
}
$adapter->setMetadata($this->key, $metadata);

/**
* @return bool
*/
private function supportsMetadata()
{
return $this->filesystem->getAdapter() instanceof MetadataSupporter;
return true;
}
}

0 comments on commit 568a0c4

Please sign in to comment.