Passed
Pull Request — master (#90)
by Domenico
02:50
created
src/XliffUtils/CheckPipeline/CheckMateCATConverter.php 1 patch
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -8,20 +8,20 @@
 block discarded – undo
8 8
      *
9 9
      * @return array|null
10 10
      */
11
-    public function check( ?array $tmp = [] ): ?array {
11
+    public function check(?array $tmp = []): ?array {
12 12
         $fileType = [];
13 13
 
14
-        if ( isset( $tmp[ 0 ] ) ) {
15
-            preg_match( '#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[ 0 ], $matches );
16
-            if ( !empty( $matches ) ) {
17
-                $fileType[ 'proprietary' ]            = false;
18
-                $fileType[ 'proprietary_name' ]       = 'MateCAT Converter';
19
-                $fileType[ 'proprietary_short_name' ] = 'matecat_converter';
20
-                if ( isset( $matches[ 2 ] ) ) {
21
-                    $fileType[ 'converter_version' ] = $matches[ 2 ];
14
+        if (isset($tmp[0])) {
15
+            preg_match('#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[0], $matches);
16
+            if (!empty($matches)) {
17
+                $fileType['proprietary']            = false;
18
+                $fileType['proprietary_name']       = 'MateCAT Converter';
19
+                $fileType['proprietary_short_name'] = 'matecat_converter';
20
+                if (isset($matches[2])) {
21
+                    $fileType['converter_version'] = $matches[2];
22 22
                 } else {
23 23
                     // First converter release didn't specify version
24
-                    $fileType[ 'converter_version' ] = '1.0';
24
+                    $fileType['converter_version'] = '1.0';
25 25
                 }
26 26
 
27 27
                 return $fileType;
Please login to merge, or discard this patch.
src/XliffUtils/CheckPipeline/CheckXliffVersion2.php 1 patch
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -8,22 +8,22 @@
 block discarded – undo
8 8
      *
9 9
      * @return array|null
10 10
      */
11
-    public function check( ?array $tmp = [] ): ?array {
11
+    public function check(?array $tmp = []): ?array {
12 12
         $fileType = [];
13 13
 
14
-        if ( isset( $tmp[ 0 ] ) ) {
15
-            preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $versionMatches );
16
-            preg_match( '|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $xmlnsMatches );
14
+        if (isset($tmp[0])) {
15
+            preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($tmp[0], 0, 1000), $versionMatches);
16
+            preg_match('|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr($tmp[0], 0, 1000), $xmlnsMatches);
17 17
 
18
-            if ( !empty( $versionMatches ) && !empty( $xmlnsMatches ) ) {
19
-                $version = $versionMatches[ 1 ];
20
-                $xmlns   = $xmlnsMatches[ 1 ];
18
+            if (!empty($versionMatches) && !empty($xmlnsMatches)) {
19
+                $version = $versionMatches[1];
20
+                $xmlns   = $xmlnsMatches[1];
21 21
 
22
-                if ( $version === $xmlns && $version >= 2 ) {
23
-                    $fileType[ 'proprietary' ]            = false;
24
-                    $fileType[ 'proprietary_name' ]       = 'Xliff v' . $version . ' File';
25
-                    $fileType[ 'proprietary_short_name' ] = 'xliff_v2';
26
-                    $fileType[ 'converter_version' ]      = '2.0';
22
+                if ($version === $xmlns && $version >= 2) {
23
+                    $fileType['proprietary']            = false;
24
+                    $fileType['proprietary_name']       = 'Xliff v' . $version . ' File';
25
+                    $fileType['proprietary_short_name'] = 'xliff_v2';
26
+                    $fileType['converter_version']      = '2.0';
27 27
 
28 28
                     return $fileType;
29 29
                 }
Please login to merge, or discard this patch.
src/XliffUtils/CheckPipeline/CheckInterface.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -8,5 +8,5 @@
 block discarded – undo
8 8
      *
9 9
      * @return array|null
10 10
      */
11
-    public function check( ?array $tmp = [] ): ?array;
11
+    public function check(?array $tmp = []): ?array;
12 12
 }
Please login to merge, or discard this patch.
src/XliffUtils/XliffVersionDetector.php 1 patch
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -9,12 +9,12 @@  discard block
 block discarded – undo
9 9
     /**
10 10
      * @var array
11 11
      */
12
-    private static array $versions_1 = [ '1.0', '1.1', '1.2' ];
12
+    private static array $versions_1 = ['1.0', '1.1', '1.2'];
13 13
 
14 14
     /**
15 15
      * @var array
16 16
      */
17
-    private static array $versions_2 = [ '2.0', '2.1' ];
17
+    private static array $versions_2 = ['2.0', '2.1'];
18 18
 
19 19
     /**
20 20
      * @param string $xliffContent
@@ -23,16 +23,16 @@  discard block
 block discarded – undo
23 23
      * @throws NotSupportedVersionException
24 24
      * @throws NotValidFileException
25 25
      */
26
-    public static function detect( string $xliffContent ): int {
27
-        preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $xliffContent, 0, 1000 ), $versionMatches );
26
+    public static function detect(string $xliffContent): int {
27
+        preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($xliffContent, 0, 1000), $versionMatches);
28 28
 
29
-        if ( empty( $versionMatches ) ) {
30
-            throw new NotValidFileException( 'This is not a valid xliff file' );
29
+        if (empty($versionMatches)) {
30
+            throw new NotValidFileException('This is not a valid xliff file');
31 31
         }
32 32
 
33
-        $version = $versionMatches[ 1 ];
33
+        $version = $versionMatches[1];
34 34
 
35
-        return self::resolveVersion( $version );
35
+        return self::resolveVersion($version);
36 36
     }
37 37
 
38 38
     /**
@@ -41,15 +41,15 @@  discard block
 block discarded – undo
41 41
      * @return int
42 42
      * @throws NotSupportedVersionException
43 43
      */
44
-    private static function resolveVersion( string $version ): int {
45
-        if ( in_array( $version, self::$versions_1 ) ) {
44
+    private static function resolveVersion(string $version): int {
45
+        if (in_array($version, self::$versions_1)) {
46 46
             return 1;
47 47
         }
48 48
 
49
-        if ( in_array( $version, self::$versions_2 ) ) {
49
+        if (in_array($version, self::$versions_2)) {
50 50
             return 2;
51 51
         }
52 52
 
53
-        throw new NotSupportedVersionException( 'Not supported version' );
53
+        throw new NotSupportedVersionException('Not supported version');
54 54
     }
55 55
 }
Please login to merge, or discard this patch.
src/XliffReplacer/XliffReplacerFactory.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -28,14 +28,14 @@
 block discarded – undo
28 28
             ?LoggerInterface                $logger = null,
29 29
             ?XliffReplacerCallbackInterface $callback = null
30 30
     ): AbstractXliffReplacer {
31
-        $info = XliffProprietaryDetect::getInfo( $originalXliffPath );
31
+        $info = XliffProprietaryDetect::getInfo($originalXliffPath);
32 32
 
33
-        if ( $info[ 'version' ] == 1 && $info[ 'proprietary_short_name' ] !== 'trados' ) {
34
-            return new Xliff12( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
35
-        } elseif ( $info[ 'version' ] == 2 ) {
36
-            return new Xliff20( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
33
+        if ($info['version'] == 1 && $info['proprietary_short_name'] !== 'trados') {
34
+            return new Xliff12($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
35
+        } elseif ($info['version'] == 2) {
36
+            return new Xliff20($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
37 37
         }
38 38
 
39
-        return new XliffSdl( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
39
+        return new XliffSdl($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
40 40
     }
41 41
 }
Please login to merge, or discard this patch.
src/XliffReplacer/XliffReplacerCallbackInterface.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -12,5 +12,5 @@
 block discarded – undo
12 12
      *
13 13
      * @return bool
14 14
      */
15
-    public function thereAreErrors( int $segmentId, string $segment, string $translation, ?array $dataRefMap = [], ?string $error = null ): bool;
15
+    public function thereAreErrors(int $segmentId, string $segment, string $translation, ?array $dataRefMap = [], ?string $error = null): bool;
16 16
 }
Please login to merge, or discard this patch.
src/XliffReplacer/StatusToStateAttribute.php 1 patch
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
             ?string $lastMrkState = null
27 27
     ): array {
28 28
 
29
-        $status       = empty( $status ) ? TranslationStatus::STATUS_TRANSLATED : $status;
29
+        $status = empty($status) ? TranslationStatus::STATUS_TRANSLATED : $status;
30 30
 
31 31
         $stateLevelsMap = [
32 32
                 TranslationStatus::STATUS_APPROVED2  => 100,
@@ -37,26 +37,26 @@  discard block
 block discarded – undo
37 37
                 TranslationStatus::STATUS_NEW        => 50
38 38
         ];
39 39
 
40
-        $orderedValues = array_flip( $stateLevelsMap );
40
+        $orderedValues = array_flip($stateLevelsMap);
41 41
 
42 42
         // Define state mappings for different statuses
43 43
         $stateMap = [
44
-                TranslationStatus::STATUS_APPROVED2  => [ "state=\"final\"", TranslationStatus::STATUS_APPROVED2 ],
44
+                TranslationStatus::STATUS_APPROVED2  => ["state=\"final\"", TranslationStatus::STATUS_APPROVED2],
45 45
                 TranslationStatus::STATUS_APPROVED   => [
46
-                        ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"",
46
+                        ($xliffVersion === 2) ? "state=\"reviewed\"" : "state=\"signed-off\"",
47 47
                         TranslationStatus::STATUS_APPROVED
48 48
                 ],
49
-                TranslationStatus::STATUS_TRANSLATED => [ "state=\"translated\"", TranslationStatus::STATUS_TRANSLATED ],
49
+                TranslationStatus::STATUS_TRANSLATED => ["state=\"translated\"", TranslationStatus::STATUS_TRANSLATED],
50 50
                 TranslationStatus::STATUS_REJECTED   => [
51
-                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"",
51
+                        ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"needs-review-translation\"",
52 52
                         TranslationStatus::STATUS_REJECTED
53 53
                 ],
54 54
                 TranslationStatus::STATUS_NEW        => [
55
-                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
55
+                        ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"",
56 56
                         TranslationStatus::STATUS_NEW
57 57
                 ],
58 58
                 TranslationStatus::STATUS_DRAFT      => [
59
-                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
59
+                        ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"",
60 60
                         TranslationStatus::STATUS_DRAFT
61 61
                 ],
62 62
         ];
@@ -67,14 +67,14 @@  discard block
 block discarded – undo
67 67
         // ---> $seg[ 'status' ] is NULL
68 68
         // If lastMrkState is empty
69 69
         $minStatus = min(
70
-                $stateLevelsMap[ $status ],
71
-                ( $stateLevelsMap[ $lastMrkState ] ?? $stateLevelsMap[ TranslationStatus::STATUS_NEW ] )
70
+                $stateLevelsMap[$status],
71
+                ($stateLevelsMap[$lastMrkState] ?? $stateLevelsMap[TranslationStatus::STATUS_NEW])
72 72
         );
73 73
 
74 74
         // If the last mark state is set, get the minimum value, otherwise get the current state
75
-        [ $state_prop, $lastMrkState ] = empty( $lastMrkState ) ? $stateMap[ $status ] : $stateMap[ $orderedValues[ $minStatus ] ];
75
+        [$state_prop, $lastMrkState] = empty($lastMrkState) ? $stateMap[$status] : $stateMap[$orderedValues[$minStatus]];
76 76
 
77
-        return [ $state_prop, $lastMrkState ];
77
+        return [$state_prop, $lastMrkState];
78 78
 
79 79
     }
80 80
 
Please login to merge, or discard this patch.
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/XliffReplacer/AbstractXliffReplacer.php 1 patch
Spacing   +114 added lines, -114 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,12 +235,12 @@  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
-        if ( is_resource( $this->originalFP ) ) {
239
-            fclose( $this->originalFP );
238
+        if (is_resource($this->originalFP)) {
239
+            fclose($this->originalFP);
240 240
         }
241 241
 
242
-        if ( is_resource( $this->outputFP ) ) {
243
-            fclose( $this->outputFP );
242
+        if (is_resource($this->outputFP)) {
243
+            fclose($this->outputFP);
244 244
         }
245 245
 
246 246
     }
@@ -251,11 +251,11 @@  discard block
 block discarded – undo
251 251
      * @return resource
252 252
      */
253 253
     protected function initSaxParser() {
254
-        $xmlSaxParser = xml_parser_create( 'UTF-8' );
255
-        xml_set_object( $xmlSaxParser, $this );
256
-        xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false );
257
-        xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' );
258
-        xml_set_character_data_handler( $xmlSaxParser, 'characterData' );
254
+        $xmlSaxParser = xml_parser_create('UTF-8');
255
+        xml_set_object($xmlSaxParser, $this);
256
+        xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false);
257
+        xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose');
258
+        xml_set_character_data_handler($xmlSaxParser, 'characterData');
259 259
 
260 260
         return $xmlSaxParser;
261 261
     }
@@ -263,8 +263,8 @@  discard block
 block discarded – undo
263 263
     /**
264 264
      * @param resource $xmlSaxParser
265 265
      */
266
-    protected function closeSaxParser( $xmlSaxParser ) {
267
-        xml_parser_free( $xmlSaxParser );
266
+    protected function closeSaxParser($xmlSaxParser) {
267
+        xml_parser_free($xmlSaxParser);
268 268
     }
269 269
 
270 270
     /**
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
      *
275 275
      * @return mixed
276 276
      */
277
-    abstract protected function tagOpen( $parser, string $name, array $attr );
277
+    abstract protected function tagOpen($parser, string $name, array $attr);
278 278
 
279 279
     /**
280 280
      * @param resource $parser
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
      *
283 283
      * @return mixed
284 284
      */
285
-    abstract protected function tagClose( $parser, string $name );
285
+    abstract protected function tagClose($parser, string $name);
286 286
 
287 287
     /**
288 288
      * @param resource $parser
@@ -290,11 +290,11 @@  discard block
 block discarded – undo
290 290
      *
291 291
      * @return mixed
292 292
      */
293
-    protected function characterData( $parser, string $data ): void {
293
+    protected function characterData($parser, string $data): void {
294 294
         // don't write <target> data
295
-        if ( !$this->inTarget && !$this->bufferIsActive ) {
296
-            $this->postProcAndFlush( $this->outputFP, $data );
297
-        } elseif ( $this->bufferIsActive ) {
295
+        if (!$this->inTarget && !$this->bufferIsActive) {
296
+            $this->postProcAndFlush($this->outputFP, $data);
297
+        } elseif ($this->bufferIsActive) {
298 298
             $this->CDATABuffer .= $data;
299 299
         }
300 300
     }
@@ -306,19 +306,19 @@  discard block
 block discarded – undo
306 306
      * @param string   $data
307 307
      * @param bool     $treatAsCDATA
308 308
      */
309
-    protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) {
309
+    protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false) {
310 310
         //postprocess string
311
-        $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data );
312
-        $data = str_replace( '&nbsp;', ' ', $data );
313
-        if ( !$treatAsCDATA ) {
311
+        $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data);
312
+        $data = str_replace('&nbsp;', ' ', $data);
313
+        if (!$treatAsCDATA) {
314 314
             //unix2dos
315
-            $data = str_replace( "\r\n", "\r", $data );
316
-            $data = str_replace( "\n", "\r", $data );
317
-            $data = str_replace( "\r", "\r\n", $data );
315
+            $data = str_replace("\r\n", "\r", $data);
316
+            $data = str_replace("\n", "\r", $data);
317
+            $data = str_replace("\r", "\r\n", $data);
318 318
         }
319 319
 
320 320
         //flush to disk
321
-        fwrite( $fp, $data );
321
+        fwrite($fp, $data);
322 322
     }
323 323
 
324 324
     /**
@@ -327,19 +327,19 @@  discard block
 block discarded – undo
327 327
      *
328 328
      * @return void
329 329
      */
330
-    protected function handleOpenUnit( string $name, array $attr ) {
330
+    protected function handleOpenUnit(string $name, array $attr) {
331 331
 
332 332
         // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
333
-        if ( $this->tuTagName === $name ) {
333
+        if ($this->tuTagName === $name) {
334 334
             $this->inTU = true;
335 335
 
336 336
             // get id
337 337
             // trim to first 100 characters because this is the limit on Matecat's DB
338
-            $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 );
338
+            $this->currentTransUnitId = substr($attr['id'], 0, 100);
339 339
 
340 340
             // `translate` attribute can be only yes or no
341 341
             // current 'translate' attribute of the current trans-unit
342
-            $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ];
342
+            $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate'];
343 343
 
344 344
             $this->setLastTransUnitSegments();
345 345
 
@@ -353,15 +353,15 @@  discard block
 block discarded – undo
353 353
      *
354 354
      * @return string
355 355
      */
356
-    protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string {
356
+    protected function handleOpenXliffTag(string $name, array $attr, string $tag): string {
357 357
 
358 358
         // Add MateCat specific namespace.
359 359
         // Add trgLang
360
-        if ( $name === 'xliff' ) {
361
-            if ( !array_key_exists( 'xmlns:mtc', $attr ) ) {
360
+        if ($name === 'xliff') {
361
+            if (!array_key_exists('xmlns:mtc', $attr)) {
362 362
                 $tag .= ' xmlns:mtc="https://www.matecat.com" ';
363 363
             }
364
-            $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag );
364
+            $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag);
365 365
         }
366 366
 
367 367
         return $tag;
@@ -373,10 +373,10 @@  discard block
 block discarded – undo
373 373
      *
374 374
      * @return void
375 375
      */
376
-    protected function checkSetInTarget( string $name ) {
376
+    protected function checkSetInTarget(string $name) {
377 377
         // check if we are entering into a <target>
378
-        if ( 'target' === $name ) {
379
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
378
+        if ('target' === $name) {
379
+            if ($this->currentTransUnitIsTranslatable === 'no') {
380 380
                 $this->inTarget = false;
381 381
             } else {
382 382
                 $this->inTarget = true;
@@ -389,8 +389,8 @@  discard block
 block discarded – undo
389 389
      *
390 390
      * @return void
391 391
      */
392
-    protected function setInBuffer( string $name ) {
393
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
392
+    protected function setInBuffer(string $name) {
393
+        if (in_array($name, $this->nodesToBuffer)) {
394 394
             $this->bufferIsActive = true;
395 395
         }
396 396
     }
@@ -398,24 +398,24 @@  discard block
 block discarded – undo
398 398
     /**
399 399
      * @param array $seg
400 400
      */
401
-    protected function updateSegmentCounts( array $seg = [] ) {
401
+    protected function updateSegmentCounts(array $seg = []) {
402 402
 
403
-        $raw_word_count = $seg[ 'raw_word_count' ];
404
-        $eq_word_count  = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 );
403
+        $raw_word_count = $seg['raw_word_count'];
404
+        $eq_word_count  = (floor($seg['eq_word_count'] * 100) / 100);
405 405
 
406
-        $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [
406
+        $this->counts['segments_count_array'][$seg['sid']] = [
407 407
                 'raw_word_count' => $raw_word_count,
408 408
                 'eq_word_count'  => $eq_word_count,
409 409
         ];
410 410
 
411
-        $this->counts[ 'raw_word_count' ] += $raw_word_count;
412
-        $this->counts[ 'eq_word_count' ]  += $eq_word_count;
411
+        $this->counts['raw_word_count'] += $raw_word_count;
412
+        $this->counts['eq_word_count']  += $eq_word_count;
413 413
     }
414 414
 
415 415
     protected function resetCounts() {
416
-        $this->counts[ 'segments_count_array' ] = [];
417
-        $this->counts[ 'raw_word_count' ]       = 0;
418
-        $this->counts[ 'eq_word_count' ]        = 0;
416
+        $this->counts['segments_count_array'] = [];
417
+        $this->counts['raw_word_count']       = 0;
418
+        $this->counts['eq_word_count']        = 0;
419 419
     }
420 420
 
421 421
     /**
@@ -424,16 +424,16 @@  discard block
 block discarded – undo
424 424
      *
425 425
      * @return void
426 426
      */
427
-    protected function checkForSelfClosedTagAndFlush( $parser, string $tag ) {
427
+    protected function checkForSelfClosedTagAndFlush($parser, string $tag) {
428 428
 
429
-        $lastChar = $this->getLastCharacter( $parser );
429
+        $lastChar = $this->getLastCharacter($parser);
430 430
 
431 431
         //trim last space
432
-        $tag = rtrim( $tag );
432
+        $tag = rtrim($tag);
433 433
 
434 434
         //detect empty tag
435 435
         $this->isEmpty = $lastChar == '/';
436
-        if ( $this->isEmpty ) {
436
+        if ($this->isEmpty) {
437 437
             $tag .= $lastChar;
438 438
         }
439 439
 
@@ -441,11 +441,11 @@  discard block
 block discarded – undo
441 441
         $tag .= ">";
442 442
 
443 443
         //set a Buffer for the segSource Source tag
444
-        if ( $this->bufferIsActive ) { // we are opening a critical CDATA section
444
+        if ($this->bufferIsActive) { // we are opening a critical CDATA section
445 445
             //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer
446 446
             $this->CDATABuffer .= $tag;
447 447
         } else {
448
-            $this->postProcAndFlush( $this->outputFP, $tag );
448
+            $this->postProcAndFlush($this->outputFP, $tag);
449 449
         }
450 450
 
451 451
     }
@@ -470,18 +470,18 @@  discard block
 block discarded – undo
470 470
          */
471 471
         $this->lastTransUnit = [];
472 472
 
473
-        if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
473
+        if (!isset($this->transUnits[$this->currentTransUnitId])) {
474 474
             return;
475 475
         }
476 476
 
477
-        $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ];
477
+        $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId];
478 478
         $last_value        = null;
479
-        $segmentsCount     = count( $listOfSegmentsIds );
480
-        for ( $i = 0; $i < $segmentsCount; $i++ ) {
481
-            $id = $listOfSegmentsIds[ $i ];
482
-            if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) {
483
-                $last_value            = $listOfSegmentsIds[ $i ];
484
-                $this->lastTransUnit[] = $this->segments[ $id ];
479
+        $segmentsCount     = count($listOfSegmentsIds);
480
+        for ($i = 0; $i < $segmentsCount; $i++) {
481
+            $id = $listOfSegmentsIds[$i];
482
+            if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) {
483
+                $last_value            = $listOfSegmentsIds[$i];
484
+                $this->lastTransUnit[] = $this->segments[$id];
485 485
             }
486 486
         }
487 487
 
@@ -491,8 +491,8 @@  discard block
 block discarded – undo
491 491
      * @return array
492 492
      */
493 493
     protected function getCurrentSegment(): array {
494
-        if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
495
-            return $this->segments[ $this->segmentInUnitPosition ];
494
+        if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) {
495
+            return $this->segments[$this->segmentInUnitPosition];
496 496
         }
497 497
 
498 498
         return [];
Please login to merge, or discard this patch.