Passed
Pull Request — master (#90)
by Domenico
02:53
created

XliffProprietaryDetect   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 74
c 7
b 1
f 0
dl 0
loc 210
rs 10
wmc 29

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getInfo() 0 6 1
A getInfoFromTmp() 0 17 2
A runPipeline() 0 8 1
A getInfoFromXliffContent() 0 5 1
A reset() 0 6 1
A getInfoByStringData() 0 16 1
C fileMustBeConverted() 0 48 12
A getFirst1024CharsFromString() 0 6 2
A getFirst1024CharsFromFile() 0 10 3
A getFirst1024CharsFromXliff() 0 7 3
A checkVersion() 0 3 2
1
<?php
2
3
namespace Matecat\XliffParser\XliffUtils;
4
5
use Exception;
6
use Matecat\XliffParser\Exception\NotSupportedVersionException;
7
use Matecat\XliffParser\Exception\NotValidFileException;
8
use Matecat\XliffParser\Utils\Files;
9
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckGlobalSight;
10
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckMateCATConverter;
11
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckSDL;
12
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckXliffVersion2;
13
14
class XliffProprietaryDetect {
15
    /**
16
     * @var array
17
     */
18
    protected static array $fileType = [];
19
20
    /**
21
     * @param string $xliffContent
22
     *
23
     * @return array
24
     */
25
    public static function getInfoFromXliffContent( string $xliffContent ): array {
26
        self::reset();
27
        $tmp = self::getFirst1024CharsFromXliff( $xliffContent );
28
29
        return self::getInfoFromTmp( $tmp );
30
    }
31
32
    /**
33
     * @param string $fullPathToFile
34
     *
35
     * @return array
36
     */
37
    public static function getInfo( string $fullPathToFile ): array {
38
        self::reset();
39
        $tmp                      = self::getFirst1024CharsFromXliff( null, $fullPathToFile );
40
        self::$fileType[ 'info' ] = Files::pathInfo( $fullPathToFile );
41
42
        return self::getInfoFromTmp( $tmp );
43
    }
44
45
    /**
46
     * @param array $tmp
47
     *
48
     * @return array
49
     */
50
    private static function getInfoFromTmp( array $tmp ): array {
51
        try {
52
            self::checkVersion( $tmp );
53
        } catch ( Exception $ignore ) {
54
            // do nothing
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
            // self::$fileType[ 'version' ] is left empty
56
        }
57
58
        // run CheckXliffProprietaryPipeline
59
        $pipeline = self::runPipeline( $tmp );
60
61
        self::$fileType[ 'proprietary' ]            = $pipeline[ 'proprietary' ];
62
        self::$fileType[ 'proprietary_name' ]       = $pipeline[ 'proprietary_name' ];
63
        self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ];
64
        self::$fileType[ 'converter_version' ]      = $pipeline[ 'converter_version' ];
65
66
        return self::$fileType;
67
    }
68
69
    /**
70
     * @param array|null $tmp
71
     *
72
     * @return array
73
     */
74
    private static function runPipeline( ?array $tmp = [] ): array {
75
        $pipeline = new CheckXliffProprietaryPipeline( $tmp );
76
        $pipeline->addCheck( new CheckSDL() );
77
        $pipeline->addCheck( new CheckGlobalSight() );
78
        $pipeline->addCheck( new CheckMateCATConverter() );
79
        $pipeline->addCheck( new CheckXliffVersion2() );
80
81
        return $pipeline->run();
82
    }
83
84
    /**
85
     * Reset $fileType
86
     */
87
    private static function reset() {
88
        self::$fileType = [
89
                'info'                   => [],
90
                'proprietary'            => false,
91
                'proprietary_name'       => null,
92
                'proprietary_short_name' => null
93
        ];
94
    }
95
96
    private static function getFirst1024CharsFromString( ?string $stringData ): string {
97
        if ( !empty( $stringData ) ) {
98
            return substr( $stringData, 0, 1024 );
99
        }
100
101
        return '';
102
    }
103
104
    private static function getFirst1024CharsFromFile( string $fullPathToFile ): string {
105
        $stringData = '';
106
        if ( !empty( $fullPathToFile ) && is_file( $fullPathToFile ) ) {
107
            $file_pointer = fopen( "$fullPathToFile", 'r' );
108
            // Checking Requirements (By specs, I know that xliff version is in the first 1KB)
109
            $stringData = fread( $file_pointer, 1024 );
110
            fclose( $file_pointer );
111
        }
112
113
        return $stringData;
114
115
    }
116
117
    /**
118
     * @param string|null $stringData
119
     * @param string|null $fullPathToFile
120
     *
121
     * @return string[]
122
     */
123
    private static function getFirst1024CharsFromXliff( ?string $stringData = null, string $fullPathToFile = null ): ?array {
124
        $stringData = static::getFirst1024CharsFromString( $stringData );
125
        if ( empty( $stringData ) ) {
126
            $stringData = static::getFirst1024CharsFromFile( $fullPathToFile );
0 ignored issues
show
Bug introduced by
It seems like $fullPathToFile can also be of type null; however, parameter $fullPathToFile of Matecat\XliffParser\Xlif...irst1024CharsFromFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
            $stringData = static::getFirst1024CharsFromFile( /** @scrutinizer ignore-type */ $fullPathToFile );
Loading history...
127
        }
128
129
        return !empty( $stringData ) ? [ $stringData ] : [];
130
    }
131
132
    /**
133
     * @param array $tmp
134
     *
135
     * @throws NotSupportedVersionException
136
     * @throws NotValidFileException
137
     */
138
    protected static function checkVersion( array $tmp ) {
139
        if ( isset( $tmp[ 0 ] ) ) {
140
            self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] );
141
        }
142
    }
143
144
    /**
145
     * @param string $stringData
146
     *
147
     * @return array
148
     * @throws NotSupportedVersionException
149
     * @throws NotValidFileException
150
     */
151
    public static function getInfoByStringData( string $stringData ): array {
152
        self::reset();
153
154
        $tmp                      = self::getFirst1024CharsFromXliff( $stringData );
155
        self::$fileType[ 'info' ] = [];
156
        self::checkVersion( $tmp );
157
158
        // run CheckXliffProprietaryPipeline
159
        $pipeline = self::runPipeline( $tmp );
160
161
        self::$fileType[ 'proprietary' ]            = $pipeline[ 'proprietary' ];
162
        self::$fileType[ 'proprietary_name' ]       = $pipeline[ 'proprietary_name' ];
163
        self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ];
164
        self::$fileType[ 'converter_version' ]      = $pipeline[ 'converter_version' ];
165
166
        return self::$fileType;
167
    }
168
169
    /**
170
     * @param string      $fullPath
171
     * @param boolean     $enforceOnXliff
172
     * @param string|null $filterAddress
173
     *
174
     * @return bool|int
175
     */
176
    public static function fileMustBeConverted( string $fullPath, ?bool $enforceOnXliff = false, ?string $filterAddress = null ) {
177
        $convert = true;
178
179
        $fileType       = self::getInfo( $fullPath );
180
        $memoryFileType = Files::getMemoryFileType( $fullPath );
181
182
        if ( Files::isXliff( $fullPath ) || $memoryFileType ) {
183
            if ( !empty( $filterAddress ) ) {
184
185
                //conversion enforce
186
                if ( !$enforceOnXliff ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $enforceOnXliff of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
187
188
                    //if file is not proprietary AND Enforce is disabled
189
                    //we take it as is
190
                    if ( !$fileType[ 'proprietary' ] || $memoryFileType ) {
191
                        $convert = false;
192
                        //ok don't convert a standard sdlxliff
193
                    }
194
                } else {
195
                    //if conversion enforce is active
196
                    //we force all xliff files but not files produced by SDL Studio because we can handle them
197
                    if (
198
                            $fileType[ 'proprietary_short_name' ] == 'matecat_converter'
199
                            || $fileType[ 'proprietary_short_name' ] == 'trados'
200
                            || $fileType[ 'proprietary_short_name' ] == 'xliff_v2'
201
                            || $memoryFileType
202
                    ) {
203
                        $convert = false;
204
                        //ok don't convert a standard sdlxliff
205
                    }
206
                }
207
            } elseif ( $fileType[ 'proprietary' ] ) {
208
209
                /**
210
                 * Application misconfiguration.
211
                 * upload should not be happened, but if we are here, raise an error.
212
                 * @see upload.class.php
213
                 * */
214
215
                $convert = -1;
216
                //stop execution
217
            } else {
218
                $convert = false;
219
                //ok don't convert a standard sdlxliff
220
            }
221
        }
222
223
        return $convert;
224
    }
225
}
226