Skip to content

Commit

Permalink
ENH Add generic types (#584)
Browse files Browse the repository at this point in the history
GuySartorelli authored Jan 17, 2024
1 parent 46e07eb commit 2ed92f8
Showing 9 changed files with 9 additions and 41 deletions.
4 changes: 1 addition & 3 deletions src/AssetControlExtension.php
Original file line number Diff line number Diff line change
@@ -24,11 +24,10 @@
* Assets which are no longer referenced will be flushed via explicit delete calls
* to the underlying filesystem.
*
* @property DataObject|Versioned $owner A {@see DataObject}, potentially decorated with {@see Versioned} extension.
* @extends DataExtension<DataObject&Versioned>
*/
class AssetControlExtension extends DataExtension
{

/**
* When archiving versioned dataobjects, should assets be archived with them?
* If false, assets will be deleted when the dataobject is archived.
@@ -266,7 +265,6 @@ protected function findAssets(DataObject $record)
*/
protected function isVersioned()
{
/** @var Versioned|DataObject $owner */
$owner = $this->owner;
return class_exists(Versioned::class)
&& $owner->hasExtension(Versioned::class)
5 changes: 0 additions & 5 deletions src/Dev/TestAssetStore.php
Original file line number Diff line number Diff line change
@@ -153,7 +153,6 @@ public static function getLocalPath(AssetContainer $asset, $forceProtected = fal
/** @var TestAssetStore $assetStore */
$assetStore = Injector::inst()->get(AssetStore::class);
$fileID = $assetStore->getFileID($asset->Filename, $asset->Hash, $asset->Variant);
/** @var Filesystem $filesystem */
$filesystem = $assetStore->getProtectedFilesystem();
if (!$forceProtected && !$filesystem->has($fileID)) {
$filesystem = $assetStore->getPublicFilesystem();
@@ -165,7 +164,6 @@ public static function getLocalPath(AssetContainer $asset, $forceProtected = fal

public function cleanFilename(string $filename)
{
/** @var FileIDHelper $helper */
$helper = Injector::inst()->get(HashFileIDHelper::class);
return $helper->cleanFilename($filename);
}
@@ -177,14 +175,12 @@ public function getFileID($filename, $hash, $variant = null)

public function parseFileID(string $fileID)
{
/** @var ParsedFileID $parsedFileID */
$parsedFileID = $this->getProtectedResolutionStrategy()->parseFileID($fileID);
return $parsedFileID ? $parsedFileID->getTuple() : null;
}

public function getOriginalFilename(string $fileID)
{
/** @var ParsedFileID $parsedFileID */
$parsedFiledID = $this->getPublicResolutionStrategy()->parseFileID($fileID);
return $parsedFiledID ? $parsedFiledID->getFilename() : null;
}
@@ -201,7 +197,6 @@ function (ParsedFileID $parsedFileID, Filesystem $fs) {

public function removeVariant(string $fileID)
{
/** @var ParsedFileID $parsedFileID */
$parsedFiledID = $this->getPublicResolutionStrategy()->parseFileID($fileID);
if ($parsedFiledID) {
return $this->getPublicResolutionStrategy()->buildFileID($parsedFiledID->setVariant(''));
13 changes: 5 additions & 8 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ class File extends DataObject implements AssetContainer, Thumbnail, CMSPreviewab
private static $singular_name = "File";

private static $plural_name = "Files";

/**
* Control whether images in the admin will be resampled
*
@@ -315,7 +315,6 @@ public static function find($filename)
// Split to folders and the actual filename, and traverse the structure.
$parts = array_filter(preg_split("#[/\\\\]+#", $filename ?? '') ?? []);
$parentID = 0;
/** @var File $item */
$item = null;
foreach ($parts as $part) {
$item = File::get()->filter([
@@ -754,7 +753,6 @@ protected function onAfterDelete()
public function onAfterRevertToLive()
{
// Force query of draft object and update (as source record is bound to live stage)
/** @var File $draftRecord */
if (class_exists(Versioned::class) &&
$draftRecord = Versioned::get_by_stage(self::class, Versioned::DRAFT)->byID($this->ID)
) {
@@ -847,7 +845,6 @@ public function collateDescendants($condition, &$collator)
{
if ($children = $this->Children()) {
foreach ($children as $item) {
/** @var File $item */
if (!$condition || eval("return $condition;")) {
$collator[] = $item;
}
@@ -895,7 +892,7 @@ public function getURL($grant = false)
if (!$grant && $this->canView()) {
$grant = true;
}

if ($this->File->exists()) {
return $this->File->getURL($grant);
}
@@ -1151,9 +1148,9 @@ public function validate()
* Caution: Changes to mapping doesn't apply to existing file records in the database.
* Also doesn't hook into {@link Object::getCustomClass()}.
*
* @param String File extension, without dot prefix. Use an asterisk ('*')
* @param string File extension, without dot prefix. Use an asterisk ('*')
* to specify a generic fallback if no mapping is found for an extension.
* @return String Classname for a subclass of {@link File}
* @return string Classname for a subclass of {@link File}
*/
public static function get_class_for_file_extension($ext)
{
@@ -1294,7 +1291,7 @@ public function getTag()
/**
* Get the back-link tracking objects that link to this file via HTML fields
*
* @retun ArrayList|DataObject[]
* @return ArrayList<DataObject>
*/
public function BackLinkTracking()
{
14 changes: 0 additions & 14 deletions src/Flysystem/FlysystemAssetStore.php
Original file line number Diff line number Diff line change
@@ -261,7 +261,6 @@ protected function applyToFileOnFilesystem(callable $callable, ParsedFileID $par
self::VISIBILITY_PROTECTED
];

/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

/** @var Filesystem $fs */
@@ -515,7 +514,6 @@ public function setFromStream($stream, $filename, $hash = null, $variant = null,
return $result;
}

/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

// When saving original filename, generate hash
@@ -587,7 +585,6 @@ function (ParsedFileID $parsedFileID, Filesystem $fs, FileResolutionStrategy $st
$destParsedFileID->setVariant($originParsedFileID->getVariant())
);

/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

if ($origin !== $destination) {
@@ -636,7 +633,6 @@ function (ParsedFileID $pfid, Filesystem $fs, FileResolutionStrategy $strategy)
$fs->copy($fromFileID, $toFileID);

// Set hash value for new file
/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);
if ($hash = $hasher->get($fromFileID, $fs)) {
$hasher->set($toFileID, $fs, $hash);
@@ -662,7 +658,6 @@ function (ParsedFileID $pfid, Filesystem $fs, FileResolutionStrategy $strategy)
*/
protected function deleteFromFileStore(ParsedFileID $parsedFileID, Filesystem $fs, FileResolutionStrategy $strategy)
{
/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

$deleted = false;
@@ -730,7 +725,6 @@ public function swapPublish($filename, $hash)
return;
}

/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

$parsedFileID = new ParsedFileID($filename, $hash);
@@ -742,7 +736,6 @@ public function swapPublish($filename, $hash)


// Look for files that might be overriden by publishing to destination store, those need to be stashed away
/** @var ParsedFileID $variantParsedFileID */
$swapFileIDStr = $toStrategy->buildFileID($parsedFileID);
$swapFiles = [];
if ($to->has($swapFileIDStr)) {
@@ -768,7 +761,6 @@ public function swapPublish($filename, $hash)


// Let's find all the variants on the origin store ... those need to be moved to the destination
/** @var ParsedFileID $variantParsedFileID */
foreach ($fromStrategy->findVariants($parsedFileID, $from) as $variantParsedFileID) {
// Copy via stream
$fromFileID = $variantParsedFileID->getFileID();
@@ -832,11 +824,9 @@ protected function moveBetweenFileStore(
FileResolutionStrategy $toStrategy,
$swap = false
) {
/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

// Let's find all the variants on the origin store ... those need to be moved to the destination
/** @var ParsedFileID $variantParsedFileID */
foreach ($fromStrategy->findVariants($parsedFileID, $from) as $variantParsedFileID) {
// Copy via stream
$fromFileID = $variantParsedFileID->getFileID();
@@ -1063,7 +1053,6 @@ function (
if (empty($variant)) {
// If deferring to the existing file, return the sha of the existing file,
// unless we are writing a variant (which has the same hash value as its original file)
/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);
$hash = $hasher->computeFromFile($targetFileID, $fs);
$parsedFileID = $parsedFileID->setHash($hash);
@@ -1131,7 +1120,6 @@ function (ParsedFileID $parsedFileID, Filesystem $fs, FileResolutionStrategy $st

if ($parsedFileID && $originalFileID = $parsedFileID->getFileID()) {
if ($fs->has($originalFileID)) {
/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);
$actualHash = $hasher->computeFromFile($originalFileID, $fs);

@@ -1455,8 +1443,6 @@ function (...$args) {
private function normaliseToDefaultPath(ParsedFileID $pfid, Filesystem $fs, FileResolutionStrategy $strategy)
{
$ops = [];

/** @var FileHashingService $hasher */
$hasher = Injector::inst()->get(FileHashingService::class);

// Let's make sure we are using a valid file name
4 changes: 2 additions & 2 deletions src/Folder.php
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ public function getSize()
/**
* Returns all children of this folder
*
* @return DataList
* @return DataList<File>
*/
public function myChildren()
{
@@ -191,7 +191,7 @@ public function hasChildFolders()
/**
* Get the children of this folder that are also folders.
*
* @return DataList
* @return DataList<Folder>
*/
public function ChildFolders()
{
2 changes: 0 additions & 2 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
@@ -853,7 +853,6 @@ public function manipulateImage($variant, $callback)
return $this->manipulate(
$variant,
function (AssetStore $store, $filename, $hash, $variant) use ($callback) {
/** @var Image_Backend $backend */
$backend = $this->getImageBackend();

// If backend isn't available
@@ -884,7 +883,6 @@ function (AssetStore $store, $filename, $hash, $variant) use ($callback) {
// Write from modified backend
if ($result instanceof Image_Backend) {
try {
/** @var Image_Backend $result */
$tuple = $result->writeToStore(
$store,
$filename,
2 changes: 1 addition & 1 deletion src/Shortcodes/FileLinkTracking.php
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@
* Note that since both SiteTree and File are versioned, LinkTracking and FileTracking will
* only be enabled for the Stage record.
*
* @property DataObject|FileLinkTracking $owner
* @method SilverStripe\ORM\ManyManyThroughList<File> FileTracking()
* @extends DataExtension<DataObject&static>
*/
class FileLinkTracking extends DataExtension
{
4 changes: 0 additions & 4 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
@@ -74,7 +74,6 @@ public static function get_shortcodes()
*/
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = [])
{
/** @var CacheInterface $cache */
$cache = static::getCache();
$cacheKey = static::getCacheKey($arguments, $content);
$cachedMarkup = static::getCachedMarkup($cache, $cacheKey, $arguments);
@@ -87,7 +86,6 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
$record = static::find_shortcode_record($arguments, $errorCode);
if ($errorCode) {
$fileFound = false;
/** @var ErrorPage $record */
$record = static::find_error_record($errorCode);
}
if (!$record) {
@@ -179,7 +177,6 @@ public static function find_shortcode_record($args, &$errorCode = null)
}

// Check if the file is found
/** @var File $file */
$file = DataObject::get_by_id(File::class, $args['id']);
if (!$file) {
$errorCode = 404;
@@ -240,7 +237,6 @@ public static function getCacheKey($params, $content = null)
*/
public static function getCache()
{
/** @var CacheInterface $cache */
return Injector::inst()->get(CacheInterface::class . '.FileShortcodeProvider');
}

2 changes: 0 additions & 2 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
@@ -56,7 +56,6 @@ public static function get_shortcodes()
*/
public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = [])
{
/** @var CacheInterface $cache */
$cache = static::getCache();
$cacheKey = static::getCacheKey($args, $content);
$cachedMarkup = static::getCachedMarkup($cache, $cacheKey, $args);
@@ -199,7 +198,6 @@ public static function regenerate_html_links($value)
*/
public static function getCache()
{
/** @var CacheInterface $cache */
return Injector::inst()->get(CacheInterface::class . '.ImageShortcodeProvider');
}

0 comments on commit 2ed92f8

Please sign in to comment.