ExportsScript::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Mostafaznv\PdfOptimizer\Concerns;
4
5
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Mostafaznv\PdfOptimizer\Attributes\Option;
7
use ReflectionClass;
8
9
10
trait ExportsScript
11
{
12
    public function command(?string $pathToFile = null, ?string $pathToOptimizedFile = null): array
13
    {
14
        $options = $this->options;
15
        $reflection = new ReflectionClass($this);
16
17
        foreach ($reflection->getProperties() as $property) {
18
            $attributes = $property->getAttributes(Option::class);
19
            $value = $property->getValue($this);
20
21
            if (is_null($value)) {
22
                continue;
23
            }
24
25
            $value = is_a($value, BackedEnum::class) ? $value->value : $value;
26
27
            foreach ($attributes as $attribute) {
28
                $instance = $attribute->newInstance();
29
30
                if (is_bool($value)) {
31
                    $value = $value ? 'true' : 'false';
32
                }
33
34
                $options[] = "$instance->name=$value";
35
            }
36
        }
37
38
        $command = array_merge([$this->gsBinary], $options, $this->extraOptions);
39
40
        if ($pathToOptimizedFile) {
41
            $command[] = "-sOutputFile=$pathToOptimizedFile";
42
        }
43
44
        if ($pathToFile) {
45
            $command[] = $pathToFile;
46
        }
47
48
        return $command;
49
    }
50
51
    public function toString(string $pathToFile = null, string $pathToOptimizedFile = null): string
52
    {
53
        return implode(' ', $this->command($pathToFile, $pathToOptimizedFile));
54
    }
55
}
56