Passed
Pull Request — master (#90)
by Domenico
02:57
created
src/Utils/Files.php 1 patch
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -18,15 +18,15 @@  discard block
 block discarded – undo
18 18
      *
19 19
      * @return array|mixed
20 20
      */
21
-    public static function pathInfo( string $path, ?int $options = 15 ) {
22
-        $rawPath = explode( DIRECTORY_SEPARATOR, $path );
21
+    public static function pathInfo(string $path, ?int $options = 15) {
22
+        $rawPath = explode(DIRECTORY_SEPARATOR, $path);
23 23
 
24
-        $basename = array_pop( $rawPath );
25
-        $dirname  = implode( DIRECTORY_SEPARATOR, $rawPath );
24
+        $basename = array_pop($rawPath);
25
+        $dirname  = implode(DIRECTORY_SEPARATOR, $rawPath);
26 26
 
27
-        $explodedFileName = explode( ".", $basename );
28
-        $extension        = strtolower( array_pop( $explodedFileName ) );
29
-        $filename         = implode( ".", $explodedFileName );
27
+        $explodedFileName = explode(".", $basename);
28
+        $extension        = strtolower(array_pop($explodedFileName));
29
+        $filename         = implode(".", $explodedFileName);
30 30
 
31 31
         $returnArray = [];
32 32
 
@@ -39,17 +39,17 @@  discard block
 block discarded – undo
39 39
 
40 40
         // foreach flag, add in $return_array the corresponding field,
41 41
         // obtained by variable name correspondence
42
-        foreach ( $flagMap as $field => $i ) {
42
+        foreach ($flagMap as $field => $i) {
43 43
             //binary AND
44
-            if ( ( $options & $i ) > 0 ) {
44
+            if (($options & $i) > 0) {
45 45
                 //variable substitution: $field can be one between 'dirname', 'basename', 'extension', 'filename'
46 46
                 // $$field gets the value of the variable named $field
47
-                $returnArray[ $field ] = $$field;
47
+                $returnArray[$field] = $$field;
48 48
             }
49 49
         }
50 50
 
51
-        if ( count( $returnArray ) == 1 ) {
52
-            $returnArray = array_pop( $returnArray );
51
+        if (count($returnArray) == 1) {
52
+            $returnArray = array_pop($returnArray);
53 53
         }
54 54
 
55 55
         return $returnArray;
@@ -60,14 +60,14 @@  discard block
 block discarded – undo
60 60
      *
61 61
      * @return ?string
62 62
      */
63
-    public static function getExtension( $path ): ?string {
64
-        $pathInfo = self::pathInfo( $path );
63
+    public static function getExtension($path): ?string {
64
+        $pathInfo = self::pathInfo($path);
65 65
 
66
-        if ( empty( $pathInfo ) ) {
66
+        if (empty($pathInfo)) {
67 67
             return null;
68 68
         }
69 69
 
70
-        return strtolower( $pathInfo[ 'extension' ] );
70
+        return strtolower($pathInfo['extension']);
71 71
     }
72 72
 
73 73
     /**
@@ -75,14 +75,14 @@  discard block
 block discarded – undo
75 75
      *
76 76
      * @return bool
77 77
      */
78
-    public static function isXliff( ?string $path ): bool {
79
-        $extension = self::getExtension( $path );
78
+    public static function isXliff(?string $path): bool {
79
+        $extension = self::getExtension($path);
80 80
 
81
-        if ( !$extension ) {
81
+        if (!$extension) {
82 82
             return false;
83 83
         }
84 84
 
85
-        switch ( $extension ) {
85
+        switch ($extension) {
86 86
             case 'xliff':
87 87
             case 'sdlxliff':
88 88
             case 'tmx':
@@ -98,14 +98,14 @@  discard block
 block discarded – undo
98 98
      *
99 99
      * @return bool|string
100 100
      */
101
-    public static function getMemoryFileType( string $path ) {
102
-        $pathInfo = self::pathInfo( $path );
101
+    public static function getMemoryFileType(string $path) {
102
+        $pathInfo = self::pathInfo($path);
103 103
 
104
-        if ( empty( $pathInfo ) ) {
104
+        if (empty($pathInfo)) {
105 105
             return false;
106 106
         }
107 107
 
108
-        switch ( strtolower( $pathInfo[ 'extension' ] ) ) {
108
+        switch (strtolower($pathInfo['extension'])) {
109 109
             case 'tmx':
110 110
                 return 'tmx';
111 111
             case 'g':
@@ -120,8 +120,8 @@  discard block
 block discarded – undo
120 120
      *
121 121
      * @return bool
122 122
      */
123
-    public static function isTMXFile( $path ): bool {
124
-        return self::getMemoryFileType( $path ) === 'tmx';
123
+    public static function isTMXFile($path): bool {
124
+        return self::getMemoryFileType($path) === 'tmx';
125 125
     }
126 126
 
127 127
     /**
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
      *
130 130
      * @return bool
131 131
      */
132
-    public static function isGlossaryFile( $path ): bool {
133
-        return self::getMemoryFileType( $path ) === 'glossary';
132
+    public static function isGlossaryFile($path): bool {
133
+        return self::getMemoryFileType($path) === 'glossary';
134 134
     }
135 135
 }
Please login to merge, or discard this patch.
src/XliffUtils/XliffProprietaryDetect.php 1 patch
Spacing   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -22,11 +22,11 @@  discard block
 block discarded – undo
22 22
      *
23 23
      * @return array
24 24
      */
25
-    public static function getInfoFromXliffContent( string $xliffContent ): array {
25
+    public static function getInfoFromXliffContent(string $xliffContent): array {
26 26
         self::reset();
27
-        $tmp = self::getFirst1024CharsFromXliff( $xliffContent );
27
+        $tmp = self::getFirst1024CharsFromXliff($xliffContent);
28 28
 
29
-        return self::getInfoFromTmp( $tmp );
29
+        return self::getInfoFromTmp($tmp);
30 30
     }
31 31
 
32 32
     /**
@@ -34,12 +34,12 @@  discard block
 block discarded – undo
34 34
      *
35 35
      * @return array
36 36
      */
37
-    public static function getInfo( string $fullPathToFile ): array {
37
+    public static function getInfo(string $fullPathToFile): array {
38 38
         self::reset();
39
-        $tmp                      = self::getFirst1024CharsFromXliff( null, $fullPathToFile );
40
-        self::$fileType[ 'info' ] = Files::pathInfo( $fullPathToFile );
39
+        $tmp                      = self::getFirst1024CharsFromXliff(null, $fullPathToFile);
40
+        self::$fileType['info'] = Files::pathInfo($fullPathToFile);
41 41
 
42
-        return self::getInfoFromTmp( $tmp );
42
+        return self::getInfoFromTmp($tmp);
43 43
     }
44 44
 
45 45
     /**
@@ -47,21 +47,21 @@  discard block
 block discarded – undo
47 47
      *
48 48
      * @return array
49 49
      */
50
-    private static function getInfoFromTmp( array $tmp ): array {
50
+    private static function getInfoFromTmp(array $tmp): array {
51 51
         try {
52
-            self::checkVersion( $tmp );
53
-        } catch ( Exception $ignore ) {
52
+            self::checkVersion($tmp);
53
+        } catch (Exception $ignore) {
54 54
             // do nothing
55 55
             // self::$fileType[ 'version' ] is left empty
56 56
         }
57 57
 
58 58
         // run CheckXliffProprietaryPipeline
59
-        $pipeline = self::runPipeline( $tmp );
59
+        $pipeline = self::runPipeline($tmp);
60 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' ];
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 65
 
66 66
         return self::$fileType;
67 67
     }
@@ -71,12 +71,12 @@  discard block
 block discarded – undo
71 71
      *
72 72
      * @return array
73 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() );
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 80
 
81 81
         return $pipeline->run();
82 82
     }
@@ -93,21 +93,21 @@  discard block
 block discarded – undo
93 93
         ];
94 94
     }
95 95
 
96
-    private static function getFirst1024CharsFromString( ?string $stringData ): string {
97
-        if ( !empty( $stringData ) ) {
98
-            return substr( $stringData, 0, 1024 );
96
+    private static function getFirst1024CharsFromString(?string $stringData): string {
97
+        if (!empty($stringData)) {
98
+            return substr($stringData, 0, 1024);
99 99
         }
100 100
 
101 101
         return '';
102 102
     }
103 103
 
104
-    private static function getFirst1024CharsFromFile( string $fullPathToFile ): string {
104
+    private static function getFirst1024CharsFromFile(string $fullPathToFile): string {
105 105
         $stringData = '';
106
-        if ( !empty( $fullPathToFile ) && is_file( $fullPathToFile ) ) {
107
-            $file_pointer = fopen( "$fullPathToFile", 'r' );
106
+        if (!empty($fullPathToFile) && is_file($fullPathToFile)) {
107
+            $file_pointer = fopen("$fullPathToFile", 'r');
108 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 );
109
+            $stringData = fread($file_pointer, 1024);
110
+            fclose($file_pointer);
111 111
         }
112 112
 
113 113
         return $stringData;
@@ -120,13 +120,13 @@  discard block
 block discarded – undo
120 120
      *
121 121
      * @return string[]
122 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 );
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);
127 127
         }
128 128
 
129
-        return !empty( $stringData ) ? [ $stringData ] : [];
129
+        return !empty($stringData) ? [$stringData] : [];
130 130
     }
131 131
 
132 132
     /**
@@ -135,9 +135,9 @@  discard block
 block discarded – undo
135 135
      * @throws NotSupportedVersionException
136 136
      * @throws NotValidFileException
137 137
      */
138
-    protected static function checkVersion( array $tmp ) {
139
-        if ( isset( $tmp[ 0 ] ) ) {
140
-            self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] );
138
+    protected static function checkVersion(array $tmp) {
139
+        if (isset($tmp[0])) {
140
+            self::$fileType['version'] = XliffVersionDetector::detect($tmp[0]);
141 141
         }
142 142
     }
143 143
 
@@ -148,20 +148,20 @@  discard block
 block discarded – undo
148 148
      * @throws NotSupportedVersionException
149 149
      * @throws NotValidFileException
150 150
      */
151
-    public static function getInfoByStringData( string $stringData ): array {
151
+    public static function getInfoByStringData(string $stringData): array {
152 152
         self::reset();
153 153
 
154
-        $tmp                      = self::getFirst1024CharsFromXliff( $stringData );
155
-        self::$fileType[ 'info' ] = [];
156
-        self::checkVersion( $tmp );
154
+        $tmp                      = self::getFirst1024CharsFromXliff($stringData);
155
+        self::$fileType['info'] = [];
156
+        self::checkVersion($tmp);
157 157
 
158 158
         // run CheckXliffProprietaryPipeline
159
-        $pipeline = self::runPipeline( $tmp );
159
+        $pipeline = self::runPipeline($tmp);
160 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' ];
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 165
 
166 166
         return self::$fileType;
167 167
     }
@@ -173,21 +173,21 @@  discard block
 block discarded – undo
173 173
      *
174 174
      * @return bool|int
175 175
      */
176
-    public static function fileMustBeConverted( string $fullPath, ?bool $enforceOnXliff = false, ?string $filterAddress = null ) {
176
+    public static function fileMustBeConverted(string $fullPath, ?bool $enforceOnXliff = false, ?string $filterAddress = null) {
177 177
         $convert = true;
178 178
 
179
-        $fileType       = self::getInfo( $fullPath );
180
-        $memoryFileType = Files::getMemoryFileType( $fullPath );
179
+        $fileType       = self::getInfo($fullPath);
180
+        $memoryFileType = Files::getMemoryFileType($fullPath);
181 181
 
182
-        if ( Files::isXliff( $fullPath ) || $memoryFileType ) {
183
-            if ( !empty( $filterAddress ) ) {
182
+        if (Files::isXliff($fullPath) || $memoryFileType) {
183
+            if (!empty($filterAddress)) {
184 184
 
185 185
                 //conversion enforce
186
-                if ( !$enforceOnXliff ) {
186
+                if (!$enforceOnXliff) {
187 187
 
188 188
                     //if file is not proprietary AND Enforce is disabled
189 189
                     //we take it as is
190
-                    if ( !$fileType[ 'proprietary' ] || $memoryFileType ) {
190
+                    if (!$fileType['proprietary'] || $memoryFileType) {
191 191
                         $convert = false;
192 192
                         //ok don't convert a standard sdlxliff
193 193
                     }
@@ -195,16 +195,16 @@  discard block
 block discarded – undo
195 195
                     //if conversion enforce is active
196 196
                     //we force all xliff files but not files produced by SDL Studio because we can handle them
197 197
                     if (
198
-                            $fileType[ 'proprietary_short_name' ] == 'matecat_converter'
199
-                            || $fileType[ 'proprietary_short_name' ] == 'trados'
200
-                            || $fileType[ 'proprietary_short_name' ] == 'xliff_v2'
198
+                            $fileType['proprietary_short_name'] == 'matecat_converter'
199
+                            || $fileType['proprietary_short_name'] == 'trados'
200
+                            || $fileType['proprietary_short_name'] == 'xliff_v2'
201 201
                             || $memoryFileType
202 202
                     ) {
203 203
                         $convert = false;
204 204
                         //ok don't convert a standard sdlxliff
205 205
                     }
206 206
                 }
207
-            } elseif ( $fileType[ 'proprietary' ] ) {
207
+            } elseif ($fileType['proprietary']) {
208 208
 
209 209
                 /**
210 210
                  * Application misconfiguration.
Please login to merge, or discard this patch.
src/XliffReplacer/AbstractXliffReplacer.php 1 patch
Spacing   +112 added lines, -112 removed lines patch added patch discarded remove patch
@@ -7,26 +7,26 @@  discard block
 block discarded – undo
7 7
 
8 8
 abstract class AbstractXliffReplacer {
9 9
     protected $originalFP;
10
-    protected $outputFP;                  // output stream pointer
10
+    protected $outputFP; // output stream pointer
11 11
 
12
-    protected string $tuTagName;                 // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*)
13
-    protected bool   $inTU             = false;  // flag to check whether we are in a <trans-unit>
14
-    protected bool   $inTarget         = false;  // flag to check whether we are in a <target>, to ignore everything
15
-    protected bool   $isEmpty          = false;  // flag to check whether we are in an empty tag (<tag/>)
16
-    protected bool   $targetWasWritten = false;  // flag to check is <target> was written in the current unit
17
-    protected string $CDATABuffer      = "";       // buffer for special tag
18
-    protected bool   $bufferIsActive   = false;    // buffer for special tag
12
+    protected string $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*)
13
+    protected bool   $inTU             = false; // flag to check whether we are in a <trans-unit>
14
+    protected bool   $inTarget         = false; // flag to check whether we are in a <target>, to ignore everything
15
+    protected bool   $isEmpty          = false; // flag to check whether we are in an empty tag (<tag/>)
16
+    protected bool   $targetWasWritten = false; // flag to check is <target> was written in the current unit
17
+    protected string $CDATABuffer      = ""; // buffer for special tag
18
+    protected bool   $bufferIsActive   = false; // buffer for special tag
19 19
 
20
-    protected int $offset = 0;         // offset for SAX pointer
20
+    protected int $offset = 0; // offset for SAX pointer
21 21
 
22
-    protected string  $currentBuffer;             // the current piece of text it's been parsed
23
-    protected int     $len;                       // length of the currentBuffer
24
-    protected array   $segments;                  // array of translations
22
+    protected string  $currentBuffer; // the current piece of text it's been parsed
23
+    protected int     $len; // length of the currentBuffer
24
+    protected array   $segments; // array of translations
25 25
     protected array   $lastTransUnit                  = [];
26 26
     protected int     $segmentInUnitPosition          = 0;
27
-    protected ?string $currentTransUnitId             = null;        // id of current <trans-unit>
27
+    protected ?string $currentTransUnitId             = null; // id of current <trans-unit>
28 28
     protected ?string $currentTransUnitIsTranslatable = null; // 'translate' attribute of current <trans-unit>
29
-    protected bool    $hasWrittenCounts               = false;  // check if <unit> already wrote segment counts (forXliff v 2.*)
29
+    protected bool    $hasWrittenCounts               = false; // check if <unit> already wrote segment counts (forXliff v 2.*)
30 30
     protected string  $targetLang;
31 31
     protected bool    $sourceInTarget                 = false;
32 32
 
@@ -76,10 +76,10 @@  discard block
 block discarded – undo
76 76
             XliffReplacerCallbackInterface $callback = null
77 77
     ) {
78 78
         self::$INTERNAL_TAG_PLACEHOLDER = $this->getInternalTagPlaceholder();
79
-        $this->createOutputFileIfDoesNotExist( $outputFilePath );
80
-        $this->setFileDescriptors( $originalXliffPath, $outputFilePath );
79
+        $this->createOutputFileIfDoesNotExist($outputFilePath);
80
+        $this->setFileDescriptors($originalXliffPath, $outputFilePath);
81 81
         $this->xliffVersion   = $xliffVersion;
82
-        $this->tuTagName      = ( $this->xliffVersion === 2 ) ? 'unit' : 'trans-unit';
82
+        $this->tuTagName      = ($this->xliffVersion === 2) ? 'unit' : 'trans-unit';
83 83
         $this->segments       = $segments;
84 84
         $this->targetLang     = $trgLang;
85 85
         $this->sourceInTarget = $setSourceInTarget;
@@ -89,17 +89,17 @@  discard block
 block discarded – undo
89 89
     }
90 90
 
91 91
     public function replaceTranslation() {
92
-        fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' );
92
+        fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>');
93 93
 
94 94
         //create Sax parser
95 95
         $xmlParser = $this->initSaxParser();
96 96
 
97
-        while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) {
97
+        while ($this->currentBuffer = fread($this->originalFP, 4096)) {
98 98
             /*
99 99
                preprocess file
100 100
              */
101 101
             // obfuscate entities because sax automatically does html_entity_decode
102
-            $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
102
+            $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
103 103
 
104 104
             //avoid cutting entities in half:
105 105
             //the last fread could have truncated an entity (say, '&lt;' in '&l'), thus invalidating the escaping
@@ -107,27 +107,27 @@  discard block
 block discarded – undo
107 107
             // 9 is the max length of an entity. So, suppose that the & is at the end of buffer,
108 108
             // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end
109 109
             //it can't be an entity, exit the loop
110
-            while ( true ) {
111
-                $_ampPos = strpos( $temporary_check_buffer, '&' );
110
+            while (true) {
111
+                $_ampPos = strpos($temporary_check_buffer, '&');
112 112
 
113 113
                 //check for real entity or escape it to safely exit from the loop!!!
114
-                if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) {
114
+                if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) {
115 115
                     break;
116 116
                 }
117 117
 
118 118
                 //if an entity is still present, fetch some more and repeat the escaping
119
-                $this->currentBuffer    .= fread( $this->originalFP, 9 );
120
-                $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
119
+                $this->currentBuffer .= fread($this->originalFP, 9);
120
+                $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
121 121
             }
122 122
 
123 123
             //free stuff outside the loop
124
-            unset( $temporary_check_buffer );
124
+            unset($temporary_check_buffer);
125 125
 
126
-            $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
127
-            $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
126
+            $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
127
+            $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
128 128
 
129 129
             //get length of chunk
130
-            $this->len = strlen( $this->currentBuffer );
130
+            $this->len = strlen($this->currentBuffer);
131 131
 
132 132
             /*
133 133
             * Get the accumulated this->offset in the document:
@@ -137,12 +137,12 @@  discard block
 block discarded – undo
137 137
             $this->offset += $this->len;
138 138
 
139 139
             //parse chunk of text
140
-            $this->runParser( $xmlParser );
140
+            $this->runParser($xmlParser);
141 141
             
142 142
         }
143 143
 
144 144
         // close Sax parser
145
-        $this->closeSaxParser( $xmlParser );
145
+        $this->closeSaxParser($xmlParser);
146 146
 
147 147
     }
148 148
 
@@ -151,15 +151,15 @@  discard block
 block discarded – undo
151 151
      *
152 152
      * @return void
153 153
      */
154
-    protected function runParser( $xmlParser ) {
154
+    protected function runParser($xmlParser) {
155 155
         //parse chunk of text
156
-        if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) {
156
+        if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) {
157 157
             //if unable, raise an exception
158
-            throw new RuntimeException( sprintf(
158
+            throw new RuntimeException(sprintf(
159 159
                     "XML error: %s at line %d",
160
-                    xml_error_string( xml_get_error_code( $xmlParser ) ),
161
-                    xml_get_current_line_number( $xmlParser )
162
-            ) );
160
+                    xml_error_string(xml_get_error_code($xmlParser)),
161
+                    xml_get_current_line_number($xmlParser)
162
+            ));
163 163
         }
164 164
     }
165 165
 
@@ -168,24 +168,24 @@  discard block
 block discarded – undo
168 168
      *
169 169
      * @return string
170 170
      */
171
-    protected function getLastCharacter( $parser ): string {
171
+    protected function getLastCharacter($parser): string {
172 172
 
173 173
         //this logic helps detecting empty tags
174 174
         //get current position of SAX pointer in all the stream of data is has read so far:
175 175
         //it points at the end of current tag
176
-        $idx = xml_get_current_byte_index( $parser );
176
+        $idx = xml_get_current_byte_index($parser);
177 177
 
178 178
         //check whether the bounds of current tag are entirely in current buffer or the end of the current tag
179 179
         //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop);
180 180
         //this check is necessary because we may have truncated a tag in half with current read,
181 181
         //and the other half may be encountered in the next buffer it will be passed
182
-        if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) {
182
+        if (isset($this->currentBuffer[$idx - $this->offset])) {
183 183
             //if this tag entire lenght fitted in the buffer, the last char must be the last
184 184
             //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/'
185
-            $lastChar = $this->currentBuffer[ $idx - $this->offset ];
185
+            $lastChar = $this->currentBuffer[$idx - $this->offset];
186 186
         } else {
187 187
             //if it's out, simple use the last character of the chunk
188
-            $lastChar = $this->currentBuffer[ $this->len - 1 ];
188
+            $lastChar = $this->currentBuffer[$this->len - 1];
189 189
         }
190 190
 
191 191
         return $lastChar;
@@ -199,19 +199,19 @@  discard block
 block discarded – undo
199 199
         return "§" .
200 200
                 substr(
201 201
                         str_replace(
202
-                                [ '+', '/' ],
202
+                                ['+', '/'],
203 203
                                 '',
204
-                                base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) )
204
+                                base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong))
205 205
                         ),
206 206
                         0,
207 207
                         4
208 208
                 );
209 209
     }
210 210
 
211
-    private function createOutputFileIfDoesNotExist( string $outputFilePath ) {
211
+    private function createOutputFileIfDoesNotExist(string $outputFilePath) {
212 212
         // create output file
213
-        if ( !file_exists( $outputFilePath ) ) {
214
-            touch( $outputFilePath );
213
+        if (!file_exists($outputFilePath)) {
214
+            touch($outputFilePath);
215 215
         }
216 216
     }
217 217
 
@@ -219,13 +219,13 @@  discard block
 block discarded – undo
219 219
      * @param string $originalXliffPath
220 220
      * @param string $outputFilePath
221 221
      */
222
-    private function setFileDescriptors( string $originalXliffPath, string $outputFilePath ) {
223
-        $this->outputFP = fopen( $outputFilePath, 'w+' );
222
+    private function setFileDescriptors(string $originalXliffPath, string $outputFilePath) {
223
+        $this->outputFP = fopen($outputFilePath, 'w+');
224 224
 
225 225
         $streamArgs = null;
226 226
 
227
-        if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) {
228
-            throw new RuntimeException( "could not open XML input" );
227
+        if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) {
228
+            throw new RuntimeException("could not open XML input");
229 229
         }
230 230
     }
231 231
 
@@ -235,8 +235,8 @@  discard block
 block discarded – undo
235 235
     public function __destruct() {
236 236
         //this stream can be closed outside the class
237 237
         //to permit multiple concurrent downloads, so suppress warnings
238
-        @fclose( $this->originalFP );
239
-        fclose( $this->outputFP );
238
+        @fclose($this->originalFP);
239
+        fclose($this->outputFP);
240 240
     }
241 241
 
242 242
     /**
@@ -245,11 +245,11 @@  discard block
 block discarded – undo
245 245
      * @return resource
246 246
      */
247 247
     protected function initSaxParser() {
248
-        $xmlSaxParser = xml_parser_create( 'UTF-8' );
249
-        xml_set_object( $xmlSaxParser, $this );
250
-        xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false );
251
-        xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' );
252
-        xml_set_character_data_handler( $xmlSaxParser, 'characterData' );
248
+        $xmlSaxParser = xml_parser_create('UTF-8');
249
+        xml_set_object($xmlSaxParser, $this);
250
+        xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false);
251
+        xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose');
252
+        xml_set_character_data_handler($xmlSaxParser, 'characterData');
253 253
 
254 254
         return $xmlSaxParser;
255 255
     }
@@ -257,8 +257,8 @@  discard block
 block discarded – undo
257 257
     /**
258 258
      * @param resource $xmlSaxParser
259 259
      */
260
-    protected function closeSaxParser( $xmlSaxParser ) {
261
-        xml_parser_free( $xmlSaxParser );
260
+    protected function closeSaxParser($xmlSaxParser) {
261
+        xml_parser_free($xmlSaxParser);
262 262
     }
263 263
 
264 264
     /**
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
      *
269 269
      * @return mixed
270 270
      */
271
-    abstract protected function tagOpen( $parser, string $name, array $attr );
271
+    abstract protected function tagOpen($parser, string $name, array $attr);
272 272
 
273 273
     /**
274 274
      * @param resource $parser
@@ -276,7 +276,7 @@  discard block
 block discarded – undo
276 276
      *
277 277
      * @return mixed
278 278
      */
279
-    abstract protected function tagClose( $parser, string $name );
279
+    abstract protected function tagClose($parser, string $name);
280 280
 
281 281
     /**
282 282
      * @param resource $parser
@@ -284,11 +284,11 @@  discard block
 block discarded – undo
284 284
      *
285 285
      * @return mixed
286 286
      */
287
-    protected function characterData( $parser, string $data ): void {
287
+    protected function characterData($parser, string $data): void {
288 288
         // don't write <target> data
289
-        if ( !$this->inTarget && !$this->bufferIsActive ) {
290
-            $this->postProcAndFlush( $this->outputFP, $data );
291
-        } elseif ( $this->bufferIsActive ) {
289
+        if (!$this->inTarget && !$this->bufferIsActive) {
290
+            $this->postProcAndFlush($this->outputFP, $data);
291
+        } elseif ($this->bufferIsActive) {
292 292
             $this->CDATABuffer .= $data;
293 293
         }
294 294
     }
@@ -300,19 +300,19 @@  discard block
 block discarded – undo
300 300
      * @param string   $data
301 301
      * @param bool     $treatAsCDATA
302 302
      */
303
-    protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) {
303
+    protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false) {
304 304
         //postprocess string
305
-        $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data );
306
-        $data = str_replace( '&nbsp;', ' ', $data );
307
-        if ( !$treatAsCDATA ) {
305
+        $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data);
306
+        $data = str_replace('&nbsp;', ' ', $data);
307
+        if (!$treatAsCDATA) {
308 308
             //unix2dos
309
-            $data = str_replace( "\r\n", "\r", $data );
310
-            $data = str_replace( "\n", "\r", $data );
311
-            $data = str_replace( "\r", "\r\n", $data );
309
+            $data = str_replace("\r\n", "\r", $data);
310
+            $data = str_replace("\n", "\r", $data);
311
+            $data = str_replace("\r", "\r\n", $data);
312 312
         }
313 313
 
314 314
         //flush to disk
315
-        fwrite( $fp, $data );
315
+        fwrite($fp, $data);
316 316
     }
317 317
 
318 318
     /**
@@ -321,19 +321,19 @@  discard block
 block discarded – undo
321 321
      *
322 322
      * @return void
323 323
      */
324
-    protected function handleOpenUnit( string $name, array $attr ) {
324
+    protected function handleOpenUnit(string $name, array $attr) {
325 325
 
326 326
         // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
327
-        if ( $this->tuTagName === $name ) {
327
+        if ($this->tuTagName === $name) {
328 328
             $this->inTU = true;
329 329
 
330 330
             // get id
331 331
             // trim to first 100 characters because this is the limit on Matecat's DB
332
-            $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 );
332
+            $this->currentTransUnitId = substr($attr['id'], 0, 100);
333 333
 
334 334
             // `translate` attribute can be only yes or no
335 335
             // current 'translate' attribute of the current trans-unit
336
-            $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ];
336
+            $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate'];
337 337
 
338 338
             $this->setLastTransUnitSegments();
339 339
 
@@ -347,15 +347,15 @@  discard block
 block discarded – undo
347 347
      *
348 348
      * @return string
349 349
      */
350
-    protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string {
350
+    protected function handleOpenXliffTag(string $name, array $attr, string $tag): string {
351 351
 
352 352
         // Add MateCat specific namespace.
353 353
         // Add trgLang
354
-        if ( $name === 'xliff' ) {
355
-            if ( !array_key_exists( 'xmlns:mtc', $attr ) ) {
354
+        if ($name === 'xliff') {
355
+            if (!array_key_exists('xmlns:mtc', $attr)) {
356 356
                 $tag .= ' xmlns:mtc="https://www.matecat.com" ';
357 357
             }
358
-            $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag );
358
+            $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag);
359 359
         }
360 360
 
361 361
         return $tag;
@@ -367,10 +367,10 @@  discard block
 block discarded – undo
367 367
      *
368 368
      * @return void
369 369
      */
370
-    protected function checkSetInTarget( string $name ) {
370
+    protected function checkSetInTarget(string $name) {
371 371
         // check if we are entering into a <target>
372
-        if ( 'target' === $name ) {
373
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
372
+        if ('target' === $name) {
373
+            if ($this->currentTransUnitIsTranslatable === 'no') {
374 374
                 $this->inTarget = false;
375 375
             } else {
376 376
                 $this->inTarget = true;
@@ -383,8 +383,8 @@  discard block
 block discarded – undo
383 383
      *
384 384
      * @return void
385 385
      */
386
-    protected function setInBuffer( string $name ) {
387
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
386
+    protected function setInBuffer(string $name) {
387
+        if (in_array($name, $this->nodesToBuffer)) {
388 388
             $this->bufferIsActive = true;
389 389
         }
390 390
     }
@@ -392,24 +392,24 @@  discard block
 block discarded – undo
392 392
     /**
393 393
      * @param array $seg
394 394
      */
395
-    protected function updateSegmentCounts( array $seg = [] ) {
395
+    protected function updateSegmentCounts(array $seg = []) {
396 396
 
397
-        $raw_word_count = $seg[ 'raw_word_count' ];
398
-        $eq_word_count  = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 );
397
+        $raw_word_count = $seg['raw_word_count'];
398
+        $eq_word_count  = (floor($seg['eq_word_count'] * 100) / 100);
399 399
 
400
-        $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [
400
+        $this->counts['segments_count_array'][$seg['sid']] = [
401 401
                 'raw_word_count' => $raw_word_count,
402 402
                 'eq_word_count'  => $eq_word_count,
403 403
         ];
404 404
 
405
-        $this->counts[ 'raw_word_count' ] += $raw_word_count;
406
-        $this->counts[ 'eq_word_count' ]  += $eq_word_count;
405
+        $this->counts['raw_word_count'] += $raw_word_count;
406
+        $this->counts['eq_word_count']  += $eq_word_count;
407 407
     }
408 408
 
409 409
     protected function resetCounts() {
410
-        $this->counts[ 'segments_count_array' ] = [];
411
-        $this->counts[ 'raw_word_count' ]       = 0;
412
-        $this->counts[ 'eq_word_count' ]        = 0;
410
+        $this->counts['segments_count_array'] = [];
411
+        $this->counts['raw_word_count']       = 0;
412
+        $this->counts['eq_word_count']        = 0;
413 413
     }
414 414
 
415 415
     /**
@@ -418,16 +418,16 @@  discard block
 block discarded – undo
418 418
      *
419 419
      * @return void
420 420
      */
421
-    protected function checkForSelfClosedTagAndFlush( $parser, string $tag ) {
421
+    protected function checkForSelfClosedTagAndFlush($parser, string $tag) {
422 422
 
423
-        $lastChar = $this->getLastCharacter( $parser );
423
+        $lastChar = $this->getLastCharacter($parser);
424 424
 
425 425
         //trim last space
426
-        $tag = rtrim( $tag );
426
+        $tag = rtrim($tag);
427 427
 
428 428
         //detect empty tag
429 429
         $this->isEmpty = $lastChar == '/';
430
-        if ( $this->isEmpty ) {
430
+        if ($this->isEmpty) {
431 431
             $tag .= $lastChar;
432 432
         }
433 433
 
@@ -435,11 +435,11 @@  discard block
 block discarded – undo
435 435
         $tag .= ">";
436 436
 
437 437
         //set a Buffer for the segSource Source tag
438
-        if ( $this->bufferIsActive ) { // we are opening a critical CDATA section
438
+        if ($this->bufferIsActive) { // we are opening a critical CDATA section
439 439
             //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer
440 440
             $this->CDATABuffer .= $tag;
441 441
         } else {
442
-            $this->postProcAndFlush( $this->outputFP, $tag );
442
+            $this->postProcAndFlush($this->outputFP, $tag);
443 443
         }
444 444
 
445 445
     }
@@ -464,18 +464,18 @@  discard block
 block discarded – undo
464 464
          */
465 465
         $this->lastTransUnit = [];
466 466
 
467
-        if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
467
+        if (!isset($this->transUnits[$this->currentTransUnitId])) {
468 468
             return;
469 469
         }
470 470
 
471
-        $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ];
471
+        $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId];
472 472
         $last_value        = null;
473
-        $segmentsCount     = count( $listOfSegmentsIds );
474
-        for ( $i = 0; $i < $segmentsCount; $i++ ) {
475
-            $id = $listOfSegmentsIds[ $i ];
476
-            if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) {
477
-                $last_value            = $listOfSegmentsIds[ $i ];
478
-                $this->lastTransUnit[] = $this->segments[ $id ];
473
+        $segmentsCount     = count($listOfSegmentsIds);
474
+        for ($i = 0; $i < $segmentsCount; $i++) {
475
+            $id = $listOfSegmentsIds[$i];
476
+            if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) {
477
+                $last_value            = $listOfSegmentsIds[$i];
478
+                $this->lastTransUnit[] = $this->segments[$id];
479 479
             }
480 480
         }
481 481
 
@@ -485,8 +485,8 @@  discard block
 block discarded – undo
485 485
      * @return array
486 486
      */
487 487
     protected function getCurrentSegment(): array {
488
-        if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
489
-            return $this->segments[ $this->segmentInUnitPosition ];
488
+        if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) {
489
+            return $this->segments[$this->segmentInUnitPosition];
490 490
         }
491 491
 
492 492
         return [];
Please login to merge, or discard this patch.