Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.2] Code analysis tweaks #589

Merged
merged 6 commits into from
Jul 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix AutoDatasource signature to match Storm
  • Loading branch information
bennothommo committed Dec 21, 2021
commit 4cf57c2da630a4638914ca4249a7b20fcb432d0b
94 changes: 25 additions & 69 deletions modules/cms/classes/AutoDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,9 @@ protected function getActiveDatasource()
}

/**
* Returns a single template.
*
* @param string $dirName
* @param string $fileName
* @param string $extension
* @return mixed
* @inheritDoc
*/
public function selectOne(string $dirName, string $fileName, string $extension)
public function selectOne(string $dirName, string $fileName, string $extension): ?array
{
try {
$path = $this->makeFilePath($dirName, $fileName, $extension);
Expand Down Expand Up @@ -359,20 +354,9 @@ public function selectOne(string $dirName, string $fileName, string $extension)
}

/**
* Returns all templates.
*
* @param string $dirName
* @param array $options Array of options, [
* 'columns' => ['fileName', 'mtime', 'content'], // Only return specific columns
* 'extensions' => ['htm', 'md', 'twig'], // Extensions to search for
* 'fileMatch' => '*gr[ae]y', // Shell matching pattern to match the filename against using the fnmatch function
* 'orders' => false // Not implemented
* 'limit' => false // Not implemented
* 'offset' => false // Not implemented
* ];
* @return array
* @inheritDoc
*/
public function select(string $dirName, array $options = [])
public function select(string $dirName, array $options = []): array
{
// Handle fileName listings through just the cache
if (@$options['columns'] === ['fileName']) {
Expand Down Expand Up @@ -412,15 +396,9 @@ public function select(string $dirName, array $options = [])
}

/**
* Creates a new template, only inserts to the active datasource
*
* @param string $dirName
* @param string $fileName
* @param string $extension
* @param string $content
* @return bool
* @inheritDoc
*/
public function insert(string $dirName, string $fileName, string $extension, string $content)
public function insert(string $dirName, string $fileName, string $extension, string $content): int
{
// Insert only on the active datasource
$result = $this->getActiveDatasource()->insert($dirName, $fileName, $extension, $content);
Expand All @@ -432,17 +410,9 @@ public function insert(string $dirName, string $fileName, string $extension, str
}

/**
* Updates an existing template.
*
* @param string $dirName
* @param string $fileName
* @param string $extension
* @param string $content
* @param string $oldFileName Defaults to null
* @param string $oldExtension Defaults to null
* @return int
* @inheritDoc
*/
public function update(string $dirName, string $fileName, string $extension, string $content, $oldFileName = null, $oldExtension = null)
public function update(string $dirName, string $fileName, string $extension, string $content, $oldFileName = null, $oldExtension = null): int
{
$searchFileName = $oldFileName ?: $fileName;
$searchExt = $oldExtension ?: $extension;
Expand Down Expand Up @@ -470,21 +440,16 @@ public function update(string $dirName, string $fileName, string $extension, str
}

/**
* Run a delete statement against the datasource, only runs delete on active datasource
*
* @param string $dirName
* @param string $fileName
* @param string $extension
* @return int
* @inheritDoc
*/
public function delete(string $dirName, string $fileName, string $extension)
public function delete(string $dirName, string $fileName, string $extension): bool
{
try {
// Delete from only the active datasource
if ($this->forceDeleting) {
$this->getActiveDatasource()->forceDelete($dirName, $fileName, $extension);
$success = $this->getActiveDatasource()->forceDelete($dirName, $fileName, $extension);
} else {
$this->getActiveDatasource()->delete($dirName, $fileName, $extension);
$success = $this->getActiveDatasource()->delete($dirName, $fileName, $extension);
}
}
catch (Exception $ex) {
Expand All @@ -501,7 +466,7 @@ public function delete(string $dirName, string $fileName, string $extension)
$this->insert($dirName, $fileName, $extension, $record['content']);

// Perform the deletion on the newly inserted record
$this->delete($dirName, $fileName, $extension);
$success = $this->delete($dirName, $fileName, $extension);
} else {
throw (new DeleteFileException)->setInvalidPath($path);
}
Expand All @@ -510,54 +475,45 @@ public function delete(string $dirName, string $fileName, string $extension)

// Refresh the cache
$this->populateCache(true);

return $success;
}

/**
* Return the last modified date of an object
*
* @param string $dirName
* @param string $fileName
* @param string $extension
* @return int
* @inheritDoc
*/
public function lastModified(string $dirName, string $fileName, string $extension)
public function lastModified(string $dirName, string $fileName, string $extension): int
{
return $this->getDatasourceForPath($this->makeFilePath($dirName, $fileName, $extension))->lastModified($dirName, $fileName, $extension);
}

/**
* Generate a cache key unique to this datasource.
*
* @param string $name
* @return string
* @inheritDoc
*/
public function makeCacheKey($name = '')
public function makeCacheKey($name = ''): string
{
$key = '';

foreach ($this->datasources as $datasource) {
$key .= $datasource->makeCacheKey($name) . '-';
}
$key .= $name;

return crc32($key);
return hash('crc32b', $key);
}

/**
* Generate a paths cache key unique to this datasource
*
* @return string
* @inheritDoc
*/
public function getPathsCacheKey()
public function getPathsCacheKey(): string
{
return 'halcyon-datastore-auto';
}

/**
* Get all available paths within this datastore
*
* @return array $paths ['path/to/file1.md' => true (path can be handled and exists), 'path/to/file2.md' => false (path can be handled but doesn't exist)]
* @inheritDoc
*/
public function getAvailablePaths()
public function getAvailablePaths(): array
{
$paths = [];
$datasources = array_reverse($this->datasources);
Expand Down