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

diego-ninja / granite / 16608963009

29 Jul 2025 10:37PM UTC coverage: 50.423%. First build
16608963009

Pull #5

github

web-flow
Merge 43d8840d7 into 6a6caca51
Pull Request #5: code: adds phpstan level max, pint with per and github actions

321 of 632 new or added lines in 77 files covered. (50.79%)

1132 of 2245 relevant lines covered (50.42%)

9.88 hits per line

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

0.0
/src/Mapping/MappingPreloader.php
1
<?php
2

3
namespace Ninja\Granite\Mapping;
4

5
use Ninja\Granite\Mapping\Exceptions\MappingException;
6

7
/**
8
 * Utility for preloading common mapping configurations.
9
 */
10
class MappingPreloader
11
{
12
    /**
13
     * Preload mapping configurations for a list of type pairs.
14
     *
15
     * @param ObjectMapper $mapper Mapper to preload for
16
     * @param array $typePairs Array of [sourceType, destinationType] pairs
17
     * @return int Number of preloaded mappings
18
     * @throws MappingException
19
     */
20
    public static function preload(ObjectMapper $mapper, array $typePairs): int
×
21
    {
22
        $count = 0;
×
23

24
        foreach ($typePairs as $pair) {
×
NEW
25
            if ( ! is_array($pair) || count($pair) < 2) {
×
NEW
26
                continue;
×
27
            }
28

NEW
29
            [$sourceType, $destinationType] = $pair;
×
30

NEW
31
            if ( ! is_string($sourceType) || ! is_string($destinationType)) {
×
NEW
32
                continue;
×
33
            }
34

35
            // Skip if already cached
36
            if ($mapper->getCache()->has($sourceType, $destinationType)) {
×
37
                continue;
×
38
            }
39

40
            // Only create mapping for valid class types
NEW
41
            if ( ! class_exists($sourceType) || ! class_exists($destinationType)) {
×
NEW
42
                continue;
×
43
            }
44

45
            // Create a mapping if needed
46
            $mapping = $mapper->createMap($sourceType, $destinationType);
×
47

48
            // Force seal to generate the mapping configuration
NEW
49
            if ( ! $mapping->isSealed()) {
×
50
                $mapping->seal();
×
51
            }
52

53
            $count++;
×
54
        }
55

56
        return $count;
×
57
    }
58

59
    /**
60
     * Preload common DTO-Entity pairs from a given namespace.
61
     *
62
     * @param ObjectMapper $mapper Mapper to preload for
63
     * @param string $namespace Namespace to scan
64
     * @param array $suffixes Array of suffixes to match (e.g. ['DTO', 'Entity'])
65
     * @return int Number of preloaded mappings
66
     * @throws MappingException
67
     */
68
    public static function preloadFromNamespace(ObjectMapper $mapper, string $namespace, array $suffixes = ['DTO', 'Entity']): int
×
69
    {
70
        $count = 0;
×
71
        $classes = self::scanNamespace($namespace);
×
72
        $pairs = [];
×
73

74
        // Group classes by base name
75
        $grouped = [];
×
76
        foreach ($classes as $class) {
×
NEW
77
            if ( ! is_string($class)) {
×
NEW
78
                continue;
×
79
            }
80

81
            foreach ($suffixes as $suffix) {
×
NEW
82
                if ( ! is_string($suffix)) {
×
NEW
83
                    continue;
×
84
                }
85

86
                if (str_ends_with($class, $suffix)) {
×
NEW
87
                    $baseName = mb_substr($class, 0, -mb_strlen($suffix));
×
88
                    $grouped[$baseName][$suffix] = $class;
×
89
                    break;
×
90
                }
91
            }
92
        }
93

94
        // Create pairs
95
        foreach ($grouped as $classes) {
×
96
            if (count($classes) >= 2) {
×
97
                $types = array_values($classes);
×
98
                $pairs[] = [$types[0], $types[1]];
×
99
                $pairs[] = [$types[1], $types[0]]; // Bidirectional
×
100
            }
101
        }
102

103
        return self::preload($mapper, $pairs);
×
104
    }
105

106
    /**
107
     * Scan a namespace for classes.
108
     *
109
     * @param string $namespace Namespace to scan
110
     * @return array Array of class names
111
     */
112
    private static function scanNamespace(string $namespace): array
×
113
    {
114
        $classes = [];
×
115

116
        $autoLoader = require dirname(__DIR__, 2) . '/vendor/autoload.php';
×
117
        $autoLoader->setPsr4($namespace . '\\', []);
×
118

119
        $prefix = $namespace . '\\';
×
NEW
120
        $prefixLen = mb_strlen($prefix);
×
121

NEW
122
        $classMap = $autoLoader->getClassMap();
×
NEW
123
        if (is_array($classMap)) {
×
NEW
124
            foreach ($classMap as $className => $filePath) {
×
NEW
125
                if (is_string($className) && 0 === strncmp($className, $prefix, $prefixLen)) {
×
NEW
126
                    $classes[] = $className;
×
127
                }
128
            }
129
        }
130

131
        return $classes;
×
132
    }
133
}
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