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

ENH Add generic types #584

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions src/AssetControlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions src/Dev/TestAssetStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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(''));
Expand Down
13 changes: 5 additions & 8 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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)
) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -895,7 +892,7 @@ public function getURL($grant = false)
if (!$grant && $this->canView()) {
$grant = true;
}

if ($this->File->exists()) {
return $this->File->getURL($grant);
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
{
Expand Down
14 changes: 0 additions & 14 deletions src/Flysystem/FlysystemAssetStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -730,7 +725,6 @@ public function swapPublish($filename, $hash)
return;
}

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

$parsedFileID = new ParsedFileID($filename, $hash);
Expand All @@ -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)) {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function getSize()
/**
* Returns all children of this folder
*
* @return DataList
* @return DataList<File>
*/
public function myChildren()
{
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 0 additions & 2 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Shortcodes/FileLinkTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 0 additions & 4 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down
2 changes: 0 additions & 2 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

Expand Down