• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / phpcpd / #22

03 May 2025 05:39PM UTC coverage: 76.552% (-0.03%) from 76.578%
#22

push

DKravtsov
phpcpd 8.2.1 release. Fixed codefragment value for xml report.

1 of 1 new or added line in 1 file covered. (100.0%)

715 of 934 relevant lines covered (76.55%)

3.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.82
/src/Log/AbstractXmlLogger.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Systemsdk\PhpCPD\Log;
6

7
use DOMDocument;
8
use Systemsdk\PhpCPD\CodeCloneMap;
9
use Systemsdk\PhpCPD\Exceptions\LoggerException;
10

11
use function file_put_contents;
12
use function mb_convert_encoding;
13
use function ord;
14
use function preg_replace;
15
use function strlen;
16

17
abstract class AbstractXmlLogger
18
{
19
    protected DOMDocument $document;
20

21
    public function __construct(
22
        private readonly string $filename
23
    ) {
24
        $this->document = new DOMDocument('1.0', 'UTF-8');
1✔
25
        $this->document->formatOutput = true;
1✔
26
    }
27

28
    abstract public function processClones(CodeCloneMap $clones): void;
29

30
    /**
31
     * @throws LoggerException
32
     */
33
    protected function flush(): void
34
    {
35
        $xml = $this->document->saveXML();
1✔
36

37
        if ($xml === false) {
1✔
38
            throw new LoggerException('Can not dump the internal xml tree back into a string');
×
39
        }
40

41
        $results = file_put_contents($this->filename, $xml);
1✔
42

43
        if ($results === false) {
1✔
44
            throw new LoggerException('Can not save log file');
×
45
        }
46
    }
47

48
    /**
49
     * @throws LoggerException
50
     */
51
    protected function convertToUtf8(string $string): string
52
    {
53
        if (!$this->isUtf8($string)) {
1✔
54
            /** @var string|false $string */
55
            $string = mb_convert_encoding($string, 'UTF-8');
×
56

57
            if ($string === false) {
×
58
                throw new LoggerException('Cannot convert to UTF-8');
×
59
            }
60
        }
61

62
        return (string)$string;
1✔
63
    }
64

65
    protected function isUtf8(string $string): bool
66
    {
67
        $length = strlen($string);
1✔
68

69
        for ($i = 0; $i < $length; $i++) {
1✔
70
            if (ord($string[$i]) < 0x80) {
1✔
71
                $n = 0;
1✔
72
            } elseif ((ord($string[$i]) & 0xE0) === 0xC0) {
×
73
                $n = 1;
×
74
            } elseif ((ord($string[$i]) & 0xF0) === 0xE0) {
×
75
                $n = 2;
×
76
            } elseif ((ord($string[$i]) & 0xF0) === 0xF0) {
×
77
                $n = 3;
×
78
            } else {
79
                return false;
×
80
            }
81

82
            for ($j = 0; $j < $n; $j++) {
1✔
83
                if ((++$i === $length) || ((ord($string[$i]) & 0xC0) !== 0x80)) {
×
84
                    return false;
×
85
                }
86
            }
87
        }
88

89
        return true;
1✔
90
    }
91

92
    /**
93
     * @throws LoggerException
94
     */
95
    protected function escapeForXml(string $string): string
96
    {
97
        $string = $this->convertToUtf8($string);
1✔
98

99
        return (string)preg_replace(
1✔
100
            '/[^\x09\x0A\x0D\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]/u',
1✔
101
            "\xEF\xBF\xBD",
1✔
102
            $string
1✔
103
        );
1✔
104
    }
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc