< Summary - Jellyfin

Information
Class: MediaBrowser.Controller.Entities.BaseItemExtensions
Assembly: MediaBrowser.Controller
File(s): /srv/git/jellyfin/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
Line coverage
59%
Covered lines: 16
Uncovered lines: 11
Coverable lines: 27
Total lines: 118
Line coverage: 59.2%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetImagePath(...)100%210%
HasImage(...)100%11100%
SetImagePath(...)100%11100%
SetImagePath(...)0%620%
DeepCopy(...)100%1212100%
DeepCopy(...)100%11100%

File(s)

/srv/git/jellyfin/MediaBrowser.Controller/Entities/BaseItemExtensions.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Linq;
 5using MediaBrowser.Model.Entities;
 6using MediaBrowser.Model.IO;
 7
 8namespace MediaBrowser.Controller.Entities
 9{
 10    public static class BaseItemExtensions
 11    {
 12        /// <summary>
 13        /// Gets the image path.
 14        /// </summary>
 15        /// <param name="item">The item.</param>
 16        /// <param name="imageType">Type of the image.</param>
 17        /// <returns>System.String.</returns>
 18        public static string GetImagePath(this BaseItem item, ImageType imageType)
 19        {
 020            return item.GetImagePath(imageType, 0);
 21        }
 22
 23        public static bool HasImage(this BaseItem item, ImageType imageType)
 24        {
 14325            return item.HasImage(imageType, 0);
 26        }
 27
 28        /// <summary>
 29        /// Sets the image path.
 30        /// </summary>
 31        /// <param name="item">The item.</param>
 32        /// <param name="imageType">Type of the image.</param>
 33        /// <param name="file">The file.</param>
 34        public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
 35        {
 236            item.SetImagePath(imageType, 0, file);
 237        }
 38
 39        /// <summary>
 40        /// Sets the image path.
 41        /// </summary>
 42        /// <param name="item">The item.</param>
 43        /// <param name="imageType">Type of the image.</param>
 44        /// <param name="file">The file.</param>
 45        public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
 46        {
 047            if (file.StartsWith("http", StringComparison.OrdinalIgnoreCase))
 48            {
 049                item.SetImage(
 050                    new ItemImageInfo
 051                    {
 052                        Path = file,
 053                        Type = imageType
 054                    },
 055                    0);
 56            }
 57            else
 58            {
 059                item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
 60            }
 061        }
 62
 63        /// <summary>
 64        /// Copies all properties on object. Skips properties that do not exist.
 65        /// </summary>
 66        /// <param name="source">The source object.</param>
 67        /// <param name="dest">The destination object.</param>
 68        /// <typeparam name="T">Source type.</typeparam>
 69        /// <typeparam name="TU">Destination type.</typeparam>
 70        public static void DeepCopy<T, TU>(this T source, TU dest)
 71            where T : BaseItem
 72            where TU : BaseItem
 73        {
 4474            ArgumentNullException.ThrowIfNull(source);
 75
 4476            ArgumentNullException.ThrowIfNull(dest);
 77
 4478            var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
 79
 941680            foreach (var sourceProp in typeof(T).GetProperties())
 81            {
 82                // We should be able to write to the property
 83                // for both the source and destination type
 84                // This is only false when the derived type hides the base member
 85                // (which we shouldn't copy anyway)
 466486                if (!sourceProp.CanRead || !sourceProp.CanWrite)
 87                {
 88                    continue;
 89                }
 90
 272891                var v = sourceProp.GetValue(source);
 272892                if (v is null)
 93                {
 94                    continue;
 95                }
 96
 132097                var p = destProps.Find(x => x.Name == sourceProp.Name);
 132098                p?.SetValue(dest, v);
 99            }
 44100        }
 101
 102        /// <summary>
 103        /// Copies all properties on newly created object. Skips properties that do not exist.
 104        /// </summary>
 105        /// <param name="source">The source object.</param>
 106        /// <typeparam name="T">Source type.</typeparam>
 107        /// <typeparam name="TU">Destination type.</typeparam>
 108        /// <returns>Destination object.</returns>
 109        public static TU DeepCopy<T, TU>(this T source)
 110            where T : BaseItem
 111            where TU : BaseItem, new()
 112        {
 44113            var dest = new TU();
 44114            source.DeepCopy(dest);
 44115            return dest;
 116        }
 117    }
 118}