#1420 Add unit attribute to FileSizeTask

Merged Jawira jawira
Coverage Reach
tasks/ext/git/GitTagTask.php tasks/ext/git/GitPullTask.php tasks/ext/git/GitBranchTask.php tasks/ext/git/GitFetchTask.php tasks/ext/git/GitPushTask.php tasks/ext/git/GitLogTask.php tasks/ext/git/GitMergeTask.php tasks/ext/git/GitCheckoutTask.php tasks/ext/git/GitDescribeTask.php tasks/ext/git/GitCloneTask.php tasks/ext/git/GitCommitTask.php tasks/ext/git/GitGcTask.php tasks/ext/git/GitArchiveTask.php tasks/ext/git/GitBaseTask.php tasks/ext/git/GitInitTask.php tasks/ext/phpunit/formatter7/PHPUnitResultFormatter7.php tasks/ext/phpunit/formatter7/PlainPHPUnitResultFormatter7.php tasks/ext/phpunit/formatter7/XMLPHPUnitResultFormatter7.php tasks/ext/phpunit/formatter7/Crap4JPHPUnitResultFormatter7.php tasks/ext/phpunit/formatter7/CloverPHPUnitResultFormatter7.php tasks/ext/phpunit/formatter7/SummaryPHPUnitResultFormatter7.php tasks/ext/phpunit/PHPUnitTask.php tasks/ext/phpunit/JUnit.php tasks/ext/phpunit/PHPUnitTestRunner7.php tasks/ext/phpunit/PHPUnitTestRunner8.php tasks/ext/phpunit/PHPUnitReportTask.php tasks/ext/phpunit/FormatterElement.php tasks/ext/phpunit/PHPUnitUtil.php tasks/ext/phpunit/BatchTest.php tasks/ext/pdo/PDOSQLExecTask.php tasks/ext/pdo/PgsqlPDOQuerySplitter.php tasks/ext/pdo/PDOSQLExecFormatterElement.php tasks/ext/pdo/DefaultPDOQuerySplitter.php tasks/ext/pdo/PDOTask.php tasks/ext/pdo/PlainPDOResultFormatter.php tasks/ext/pdo/XMLPDOResultFormatter.php tasks/ext/pdo/PDOSQLExecTransaction.php tasks/ext/pdo/DummyPDOQuerySplitter.php tasks/ext/pdo/PDOResultFormatter.php tasks/ext/pdo/PDOQuerySplitter.php tasks/ext/hg/HgAddTask.php tasks/ext/hg/HgTagTask.php tasks/ext/hg/HgLogTask.php tasks/ext/hg/HgCommitTask.php tasks/ext/hg/HgUpdateTask.php tasks/ext/hg/HgCloneTask.php tasks/ext/hg/HgRevertTask.php tasks/ext/hg/HgPushTask.php tasks/ext/hg/HgBaseTask.php tasks/ext/hg/HgInitTask.php tasks/ext/hg/HgPullTask.php tasks/ext/hg/HgArchiveTask.php tasks/ext/property/Variable.php tasks/ext/property/RegexTask.php tasks/ext/property/PathToFileSet.php tasks/ext/property/PropertySelector.php tasks/ext/property/SortList.php tasks/ext/property/URLEncodeTask.php tasks/ext/property/PropertyCopy.php tasks/ext/property/AbstractPropertySetterTask.php tasks/ext/sass/SassTask.php tasks/ext/sass/SassTaskCompilerFactory.php tasks/ext/sass/ScssPhpCompiler.php tasks/ext/sass/SassCompiler.php tasks/ext/svn/SvnBaseTask.php tasks/ext/svn/SvnListTask.php tasks/ext/svn/SvnInfoTask.php tasks/ext/svn/SvnLogTask.php tasks/ext/svn/SvnLastRevisionTask.php tasks/ext/svn/SvnCommitTask.php tasks/ext/svn/SvnPropgetTask.php tasks/ext/svn/SvnProplistTask.php tasks/ext/svn/SvnPropsetTask.php tasks/ext/svn/SvnCopyTask.php tasks/ext/svn/SvnSwitchTask.php tasks/ext/svn/SvnExportTask.php tasks/ext/svn/SvnCheckoutTask.php tasks/ext/svn/SvnUpdateTask.php tasks/ext/svn/SvnRevertTask.php tasks/ext/inifile/IniFileTask.php tasks/ext/inifile/IniFileConfig.php tasks/ext/inifile/IniFileSet.php tasks/ext/inifile/IniFileGet.php tasks/ext/inifile/IniFileRemove.php tasks/ext/zendguard/ZendGuardLicenseTask.php tasks/ext/zendguard/ZendGuardEncodeTask.php tasks/ext/zendguard/ZendGuardFileSet.php tasks/ext/sonar/SonarTask.php tasks/ext/sonar/SonarConfigurationFileParser.php tasks/ext/sonar/SonarProperty.php tasks/ext/phar/PharPackageTask.php tasks/ext/phar/PharDataTask.php tasks/ext/phar/PharMetadataElement.php tasks/ext/phar/PharMetadata.php tasks/ext/pdepend/PhpDependTask.php tasks/ext/pdepend/PhpDependAnalyzerElement.php tasks/ext/pdepend/PhpDependLoggerElement.php tasks/ext/Service/Amazon/S3/S3PutTask.php tasks/ext/Service/Amazon/S3/S3GetTask.php tasks/ext/Service/Amazon/S3.php tasks/ext/Service/Amazon.php tasks/ext/phpstan/PHPStanTask.php tasks/ext/phpstan/CommandBuilder/PHPStanAnalyseCommandBuilder.php tasks/ext/phpstan/CommandBuilder/PHPStanCommandBuilder.php tasks/ext/phpstan/CommandBuilder/PHPStanCommandBuilderFactory.php tasks/ext/phpstan/CommandBuilder/PHPStanHelpCommandBuilder.php tasks/ext/phpstan/CommandBuilder/PHPStanListCommandBuilder.php tasks/ext/phploc/PHPLocTask.php tasks/ext/phploc/PHPLocFormatterFactory.php tasks/ext/phploc/PHPLocFormatterElement.php tasks/ext/phploc/AbstractPHPLocFormatter.php tasks/ext/phploc/PHPLocTextFormatter.php tasks/ext/phploc/PHPLocCSVFormatter.php tasks/ext/phploc/PHPLocXMLFormatter.php tasks/ext/phploc/PHPLocJSONFormatter.php tasks/ext/GrowlNotifyTask.php tasks/ext/phpcpd/PHPCPDTask.php tasks/ext/phpcpd/formatter/DefaultPHPCPDResultFormatter.php tasks/ext/phpcpd/formatter/PMDPHPCPDResultFormatter.php tasks/ext/phpcpd/PHPCPDFormatterElement.php tasks/ext/dbdeploy/DbDeployTask.php tasks/ext/dbdeploy/DbmsSyntaxFactory.php tasks/ext/dbdeploy/DbmsSyntaxOracle.php tasks/ext/dbdeploy/DbmsSyntaxMysql.php tasks/ext/dbdeploy/DbmsSyntaxMsSql.php tasks/ext/dbdeploy/DbmsSyntaxSQLite.php tasks/ext/dbdeploy/DbmsSyntaxPgSQL.php tasks/ext/dbdeploy/DbmsSyntax.php tasks/ext/phpmd/PHPMDTask.php tasks/ext/phpmd/PHPMDFormatterElement.php tasks/ext/phpmd/PHPMDRendererRemoveFromCache.php tasks/ext/FileSyncTask.php tasks/ext/RSTTask.php tasks/ext/visualizer/VisualizerTask.php tasks/ext/JslLintTask.php tasks/ext/WikiPublishTask.php tasks/ext/TarTask.php tasks/ext/ZipTask.php tasks/ext/VersionTask.php tasks/ext/SymlinkTask.php tasks/ext/PhpLintTask.php tasks/ext/ManifestTask.php tasks/ext/SymfonyConsole/SymfonyConsoleTask.php tasks/ext/SymfonyConsole/Arg.php tasks/ext/PatchTask.php tasks/ext/ExtractBaseTask.php tasks/ext/hipchat/HipchatTask.php tasks/ext/ReplaceRegexpTask.php tasks/ext/MailTask.php tasks/ext/XmlLintTask.php tasks/ext/HttpGetTask.php tasks/ext/FileHashTask.php tasks/ext/HttpTask.php tasks/ext/PhpCSTask.php tasks/ext/StopwatchTask.php tasks/ext/ComposerTask.php tasks/ext/TarFileSet.php tasks/ext/HttpRequestTask.php tasks/ext/NotifySendTask.php tasks/ext/XmlPropertyTask.php tasks/ext/UntarTask.php tasks/ext/FileSizeTask.php tasks/ext/UnzipTask.php tasks/ext/ZipFileSet.php tasks/ext/ParallelTask.php tasks/ext/JsonValidateTask.php tasks/ext/AutoloaderTask.php tasks/ext/ThrowTask.php tasks/ext/PackageAsPathTask.php tasks/system/condition/ConditionBase.php tasks/system/condition/PhingVersion.php tasks/system/condition/HttpCondition.php tasks/system/condition/HasFreeSpaceCondition.php tasks/system/condition/VersionCompareCondition.php tasks/system/condition/OsCondition.php tasks/system/condition/Matches.php tasks/system/condition/IsFileSelected.php tasks/system/condition/EqualsCondition.php tasks/system/condition/ConditionEnumeration.php tasks/system/condition/ContainsCondition.php tasks/system/condition/SocketCondition.php tasks/system/condition/FilesMatch.php tasks/system/condition/ReferenceExistsCondition.php tasks/system/condition/IsSetCondition.php tasks/system/condition/IsFailure.php tasks/system/condition/NotCondition.php tasks/system/condition/IsPropertyTrueCondition.php tasks/system/condition/IsPropertyFalseCondition.php tasks/system/condition/XorCondition.php tasks/system/condition/IsTrueCondition.php tasks/system/condition/IsFalseCondition.php tasks/system/condition/NestedCondition.php tasks/system/condition/AndCondition.php tasks/system/condition/OrCondition.php tasks/system/ApplyTask.php tasks/system/PhingTask.php tasks/system/CopyTask.php tasks/system/ExecTask.php tasks/system/PropertyTask.php tasks/system/SubPhing.php tasks/system/AppendTask.php tasks/system/ForeachTask.php tasks/system/DeleteTask.php tasks/system/PathConvert.php tasks/system/EchoProperties.php tasks/system/MoveTask.php tasks/system/DependSet.php tasks/system/RecorderEntry.php tasks/system/UpToDateTask.php tasks/system/MatchingTask.php tasks/system/TouchTask.php tasks/system/AvailableTask.php tasks/system/PhpEvalTask.php tasks/system/ChownTask.php tasks/system/ChmodTask.php tasks/system/EchoTask.php tasks/system/FailTask.php tasks/system/RecorderTask.php tasks/system/ImportTask.php tasks/system/AttribTask.php tasks/system/LoadFileTask.php tasks/system/WaitForTask.php tasks/system/TruncateTask.php tasks/system/AppendTask/TextElement.php tasks/system/ReflexiveTask.php tasks/system/InputTask.php tasks/system/PhingCallTask.php tasks/system/Retry.php tasks/system/TstampTask.php tasks/system/IncludePathTask.php tasks/system/SleepTask.php tasks/system/TempFile.php tasks/system/AugmentReference.php tasks/system/IfTask.php tasks/system/SwitchTask.php tasks/system/Relentless.php tasks/system/TryCatchTask.php tasks/system/TstampCustomFormat.php tasks/system/DefaultExcludes.php tasks/system/ConditionTask.php tasks/system/TaskdefTask.php tasks/system/element/LogLevelAware.php tasks/system/MkdirTask.php tasks/system/XsltTask.php tasks/system/MapEntry.php tasks/system/EchoXML.php tasks/system/AdhocTaskdefTask.php tasks/system/ResolvePathTask.php tasks/system/Basename.php tasks/system/AdhocTypedefTask.php tasks/system/ElseIfTask.php tasks/system/AdhocTask.php tasks/system/Dirname.php tasks/system/TypedefTask.php tasks/system/DynamicTask.php tasks/system/CaseTask.php tasks/system/BlockForTask.php tasks/system/RunTargetTask.php tasks/system/SequentialTask.php tasks/system/PhingReference.php tasks/system/DiagnosticsTask.php tasks/system/WarnTask.php types/selectors/SizeSelector.php types/selectors/DateSelector.php types/selectors/ContainsSelector.php types/selectors/SelectorUtils.php types/selectors/FilenameSelector.php types/selectors/ContainsRegexpSelector.php types/selectors/SelectorAware.php types/selectors/DepthSelector.php types/selectors/AbstractSelectorContainer.php types/selectors/PresentSelector.php types/selectors/DependSelector.php types/selectors/MappingSelector.php types/selectors/TypeSelector.php types/selectors/ExtendSelector.php types/selectors/PosixPermissionsSelector.php types/selectors/MajoritySelector.php types/selectors/SelectSelector.php types/selectors/DifferentSelector.php types/selectors/NoneSelector.php types/selectors/AndSelector.php types/selectors/OrSelector.php types/selectors/BaseSelector.php types/selectors/BaseSelectorContainer.php types/selectors/NotSelector.php types/selectors/BaseExtendSelector.php types/selectors/ReadableSelector.php types/selectors/SymlinkSelector.php types/selectors/WritableSelector.php types/selectors/ExecutableSelector.php types/AbstractFileSet.php types/Path.php types/PatternSet.php types/Commandline.php types/Mapper.php types/FileList.php types/FilterChain.php types/PhingFilterReader.php types/DataType.php types/Excludes.php types/RegularExpression.php types/element/ClasspathAware.php types/element/DirSetAware.php types/element/FileListAware.php types/element/FilterChainAware.php types/element/FileSetAware.php types/PatternSetNameEntry.php types/TokenSource.php types/Parameter.php types/Description.php types/Reference.php types/environment/EnvVariable.php types/CommandlineMarker.php types/CommandlineArgument.php types/Environment.php types/PropertyValue.php types/ExcludesNameEntry.php types/PathElement.php types/FileSet.php types/TokenReader.php types/DirSet.php system/io/FileSystem.php system/io/WindowsFileSystem.php system/io/PhingFile.php system/io/UnixFileSystem.php system/io/InputStream.php system/io/BufferedReader.php system/io/XmlFileParser.php system/io/OutputStream.php system/io/YamlFileParser.php system/io/IniFileParser.php system/io/StringReader.php system/io/FileInputStream.php system/io/PrintStream.php system/io/InputStreamReader.php system/io/FileOutputStream.php system/io/FilterReader.php system/io/ConsoleReader.php system/io/BufferedWriter.php system/io/OutputStreamWriter.php system/io/FileParserFactory.php system/io/Reader.php system/io/FileReader.php system/io/FileWriter.php system/io/Writer.php system/util/Properties.php system/util/RegisterSlot.php system/util/Timer.php system/util/Register.php system/lang/EventObject.php system/lang/Character.php filters/ReplaceTokens.php filters/XsltFilter.php filters/ReplaceTokensWithFile.php filters/util/ChainReaderHelper.php filters/util/IniFileTokenReader.php filters/TranslateGettext.php filters/LineContainsRegexp.php filters/LineContains.php filters/ConcatFilter.php filters/TailFilter.php filters/XincludeFilter.php filters/StripLineComments.php filters/SuffixLines.php filters/HeadFilter.php filters/TidyFilter.php filters/IconvFilter.php filters/PrefixLines.php filters/SortFilter.php filters/PhpArrayMapLines.php filters/StripLineBreaks.php filters/BaseFilterReader.php filters/TabToSpaces.php filters/EscapeUnicode.php filters/ReplaceRegexp.php filters/StripWhitespace.php filters/StripPhpComments.php filters/XsltParam.php filters/ClassConstants.php filters/Token.php filters/ExpandProperties.php filters/BaseParamFilterReader.php filters/Comment.php filters/Contains.php listener/statistics/StatisticsReport.php listener/statistics/Table.php listener/statistics/Series.php listener/statistics/StatsTimer.php listener/statistics/SeriesTimer.php listener/statistics/TimerMap.php listener/statistics/StringFormatter.php listener/statistics/ProjectTimer.php listener/statistics/ProjectTimerMap.php listener/statistics/Duration.php listener/statistics/SeriesMap.php listener/statistics/TimeFormatter.php listener/statistics/DefaultClock.php listener/XmlLogger.php listener/DefaultLogger.php listener/HtmlColorLogger.php listener/ProgressLogger.php listener/MailLogger.php listener/AnsiColorLogger.php listener/StatisticsListener.php listener/MonologListener.php listener/JsonLogger.php listener/ProfileLogger.php listener/NoBannerLogger.php listener/TargetLogger.php listener/SilentLogger.php listener/TimestampedLogger.php util/DirectoryScanner.php util/FileUtils.php util/regexp/PregEngine.php util/regexp/Regexp.php util/SourceFileScanner.php util/ExtendedFileStream.php util/StringHelper.php util/DataStore.php util/PathTokenizer.php util/XMLFragment.php util/XMLChild.php util/LogWriter.php Phing.php parser/ProjectConfigurator.php parser/TargetHandler.php parser/ProjectHandler.php parser/ElementHandler.php parser/PhingXMLContext.php parser/ExpatParser.php parser/Location.php parser/AbstractHandler.php parser/RootHandler.php parser/AbstractSAXParser.php Project.php IntrospectionHelper.php PropertyHelper.php ComponentHelper.php mappers/GlobMapper.php mappers/RegexpMapper.php mappers/ContainerMapper.php mappers/CutDirsMapper.php mappers/ChainedMapper.php mappers/CompositeMapper.php mappers/MergeMapper.php mappers/FirstMatchMapper.php mappers/FlattenMapper.php mappers/IdentityMapper.php Target.php Task.php UnknownElement.php Diagnostics.php input/ConsoleInputHandler.php input/InputRequest.php input/MultipleChoiceInputRequest.php input/YesNoInputRequest.php input/NoInteractionInputHandler.php RuntimeConfigurable.php dispatch/DispatchUtils.php dispatch/DispatchTask.php BuildEvent.php BuildException.php TaskAdapter.php ProjectComponent.php ConfigurationException.php ExitStatusException.php ExtensionPoint.php

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.


@@ -18,16 +18,17 @@
Loading
18 18
 */
19 19
20 20
/**
21 -
 * fileHash
21 +
 * FileSizeTask
22 22
 *
23 -
 * Calculate either MD5 or SHA hash value of a specified file and retun the
24 -
 * value in a property
23 +
 * Returns the size of a file
25 24
 *
26 25
 * @author  Johan Persson <johan162@gmail.com>
27 26
 * @package phing.tasks.ext
28 27
 */
29 28
class FileSizeTask extends Task
30 29
{
30 +
    const UNITS = ['B', 'K', 'M', 'G', 'T', 'P'];
31 +
31 32
    /**
32 33
     * Property for File
33 34
     *
@@ -38,17 +39,27 @@
Loading
38 39
    /**
39 40
     * Property where the file size will be stored
40 41
     *
41 -
     * @var string $property
42 +
     * @var string
42 43
     */
43 44
    private $propertyName = "filesize";
44 45
46 +
    /**
47 +
     * Return size in this unit
48 +
     *
49 +
     * @var string
50 +
     */
51 +
    private $unit = self::UNITS[0];
52 +
45 53
    /**
46 54
     * Which file to calculate the file size of
47 55
     *
48 56
     * @param PhingFile $file
49 57
     */
50 -
    public function setFile($file)
58 +
    public function setFile(PhingFile $file)
51 59
    {
60 +
        if (!$file->canRead()) {
61 +
            throw new BuildException(sprintf('Input file does not exist or is not readable: %s', $file->getName()));
62 +
        }
52 63
        $this->file = $file;
53 64
    }
54 65
@@ -58,11 +69,23 @@
Loading
58 69
     * @param  $property
59 70
     * @return void
60 71
     */
61 -
    public function setPropertyName($property)
72 +
    public function setPropertyName(string $property)
62 73
    {
74 +
        if (empty($property)) {
75 +
            throw new BuildException('Property name cannot be empty');
76 +
        }
63 77
        $this->propertyName = $property;
64 78
    }
65 79
80 +
    public function setUnit(string $originalUnit)
81 +
    {
82 +
        $unit = strtoupper($originalUnit);
83 +
        if (!in_array($unit, self::UNITS)) {
84 +
            throw new BuildException(sprintf('Invalid unit: %s', $originalUnit));
85 +
        }
86 +
        $this->unit = $unit;
87 +
    }
88 +
66 89
    /**
67 90
     * Main-Method for the Task
68 91
     *
@@ -71,58 +94,25 @@
Loading
71 94
     */
72 95
    public function main()
73 96
    {
74 -
        $this->checkFile();
75 -
        $this->checkPropertyName();
97 +
        if (!($this->file instanceof PhingFile)) {
98 +
            throw new BuildException('Input file not specified');
99 +
        }
76 100
77 101
        $size = filesize($this->file);
78 102
79 103
        if ($size === false) {
80 -
            throw new BuildException(sprintf('[FileSize] Cannot determine size of file: %s', $this->file));
104 +
            throw new BuildException(sprintf('Cannot determine size of file: %s', $this->file));
81 105
        }
106 +
        $this->log(sprintf('%s %s', $size, self::UNITS[0]), Project::MSG_VERBOSE);
82 107
83 -
        // publish hash value
84 -
        $this->project->setProperty($this->propertyName, $size);
85 -
    }
108 +
        $size = $size / pow(1024, array_search($this->unit, self::UNITS, true));
86 109
87 -
    /**
88 -
     * checks file attribute
89 -
     *
90 -
     * @return void
91 -
     * @throws BuildException
92 -
     */
93 -
    private function checkFile()
94 -
    {
95 -
        // check File
96 -
        if (
97 -
            $this->file === null
98 -
            || strlen($this->file) == 0
99 -
        ) {
100 -
            throw new BuildException('[FileSize] You must specify an input file.', $this->file);
110 +
        if (is_float($size)) {
111 +
            $size = round($size, 2);
101 112
        }
113 +
        $this->log(sprintf('%s %s', $size, $this->unit), Project::MSG_INFO);
102 114
103 -
        if (!is_readable($this->file)) {
104 -
            throw new BuildException(
105 -
                sprintf(
106 -
                    '[FileSize] Input file does not exist or is not readable: %s',
107 -
                    $this->file
108 -
                )
109 -
            );
110 -
        }
111 -
    }
112 -
113 -
    /**
114 -
     * checks property attribute
115 -
     *
116 -
     * @return void
117 -
     * @throws BuildException
118 -
     */
119 -
    private function checkPropertyName()
120 -
    {
121 -
        if (
122 -
            null === $this->propertyName
123 -
            || strlen($this->propertyName) === 0
124 -
        ) {
125 -
            throw new BuildException('[FileSize] Property name for publishing file size is not set');
126 -
        }
115 +
        // publish hash value
116 +
        $this->project->setProperty($this->propertyName, $size);
127 117
    }
128 118
}

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Complexity Coverage
classes/phing ø 0.03% 54.14%
Project Totals (468 files) 9125 54.14%
Loading