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

bitExpert / phpstan-magento / 15082438953

17 May 2025 06:15AM UTC coverage: 88.0%. Remained the same
15082438953

push

github

shochdoerfer
Update CHANGELOG.md file

638 of 725 relevant lines covered (88.0%)

1.77 hits per line

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

94.59
/src/bitExpert/PHPStan/Magento/Autoload/DataProvider/ExtensionAttributeDataProvider.php
1
<?php
2
/*
3
 * This file is part of the phpstan-magento package.
4
 *
5
 * (c) bitExpert AG
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11

12
namespace bitExpert\PHPStan\Magento\Autoload\DataProvider;
13

14
use DOMDocument;
15
use DOMElement;
16
use DOMXPath;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\Finder\SplFileInfo;
19

20
class ExtensionAttributeDataProvider
21
{
22
    /**
23
     * @var string
24
     */
25
    private $magentoRoot;
26
    /**
27
     * @var DOMDocument[]|null
28
     */
29
    private $xmlDocs;
30

31
    /**
32
     * ExtensionAttributeDataProvider constructor.
33
     *
34
     * @param string $magentoRoot
35
     */
36
    public function __construct(string $magentoRoot)
37
    {
38
        $this->magentoRoot = $magentoRoot;
6✔
39
    }
40

41
    /**
42
     * Returns
43
     * @param string $sourceInterface
44
     * @return array<string, string>
45
     */
46
    public function getAttributesForInterface(string $sourceInterface): array
47
    {
48
        $return = [];
6✔
49

50
        foreach ($this->getExtensionAttributesXmlDocs() as $doc) {
6✔
51
            $xpath = new DOMXPath($doc);
5✔
52
            $attrs = $xpath->query(
5✔
53
                sprintf('//extension_attributes[@for="%s"]/attribute', $sourceInterface),
5✔
54
                $doc->documentElement
5✔
55
            );
5✔
56

57
            if ($attrs === false) {
5✔
58
                continue;
×
59
            }
60

61
            foreach ($attrs as $attr) {
5✔
62
                /** @var DOMElement $attr */
63
                $propertyName = $this->getAttrName($attr);
4✔
64
                $type = $this->getAttrType($attr);
4✔
65
                $return[$propertyName] = $type;
4✔
66
            }
67
        }
68

69
        return $return;
6✔
70
    }
71

72

73
    /**
74
     * Create a generator which creates DOM documents for every extension attributes XML file found.
75
     *
76
     * @return DOMDocument[]
77
     */
78
    protected function getExtensionAttributesXmlDocs(): array
79
    {
80
        if (is_array($this->xmlDocs)) {
6✔
81
            return $this->xmlDocs;
×
82
        }
83

84
        $finder = Finder::create()
6✔
85
            ->files()
6✔
86
            ->in($this->magentoRoot)
6✔
87
            ->name('extension_attributes.xml')
6✔
88
            ->filter(static function (SplFileInfo $file) {
6✔
89
                // ignore any files not located in an etc directory to exclude e.g. test data
90
                return $file->isFile() && (bool) preg_match('#etc/extension_attributes.xml$#', $file->getPathname());
6✔
91
            });
6✔
92

93
        $this->xmlDocs = [];
6✔
94
        foreach ($finder as $item) {
6✔
95
            /** @var SplFileInfo $item */
96
            $doc = new DOMDocument();
5✔
97
            $doc->loadXML($item->getContents());
5✔
98
            $this->xmlDocs[] = $doc;
5✔
99
        }
100

101
        return $this->xmlDocs;
6✔
102
    }
103

104
    /**
105
     * Extracts and formats the attribute type out of the given DOM element.
106
     *
107
     * @param DOMElement $attr
108
     * @return string
109
     */
110
    protected function getAttrType(DOMElement $attr): string
111
    {
112
        $type = $attr->getAttribute('type');
4✔
113
        $cleanType = str_replace('[]', '', $type);
4✔
114

115
        $primitiveTypes = ['float', 'int', 'string', 'bool', 'boolean'];
4✔
116
        return in_array(strtolower($cleanType), $primitiveTypes, true) ? $type : '\\'.$type;
4✔
117
    }
118

119
    /**
120
     * Extracts and formats the attribute name out of the given DOM element
121
     * @param DOMElement $attr
122
     * @return string
123
     */
124
    protected function getAttrName(DOMElement $attr): string
125
    {
126
        // see Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToCamelCase()
127
        $attrName = $attr->getAttribute('code');
4✔
128
        $attrName = str_replace('_', '', ucwords($attrName, '_'));
4✔
129
        return lcfirst($attrName);
4✔
130
    }
131
}
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