GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 6cd8a4...d9532a )
by Leonardo
03:05
created

BlockHelpers   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 83
ccs 0
cts 48
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlocksFromCustomPost() 0 28 6
A getSingleBlockOfTypeFromCustomPost() 0 9 2
A getBlocksOfTypeFromCustomPost() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLAPI\GraphQLAPI\General;
6
7
use GraphQLAPI\GraphQLAPI\Blocks\AbstractBlock;
8
use WP_Post;
9
10
class BlockHelpers
11
{
12
    /**
13
     * After parsing a post, cache its blocks
14
     *
15
     * @var array<int, array>
16
     */
17
    protected static array $blockCache = [];
18
19
    /**
20
     * Extract the blocks from the post
21
     *
22
     * @param WP_Post|int $configurationPostOrID
23
     * @return array<string, mixed> The block stores its data as property => value
24
     */
25
    public static function getBlocksFromCustomPost(
26
        $configurationPostOrID
27
    ): array {
28
        if (\is_object($configurationPostOrID)) {
29
            $configurationPost = $configurationPostOrID;
30
            $configurationPostID = $configurationPost->ID;
31
        } else {
32
            $configurationPostID = $configurationPostOrID;
33
            $configurationPost = \get_post($configurationPostID);
34
        }
35
        // If there's either no post or ID, then that object doesn't exist (or maybe it's draft or trashed)
36
        if (!$configurationPost || !$configurationPostID) {
0 ignored issues
show
introduced by
$configurationPost is of type WP_Post, thus it always evaluated to true.
Loading history...
37
            return [];
38
        }
39
        // If it's trashed, then do not use
40
        if ($configurationPost->post_status == 'trash') {
41
            return [];
42
        }
43
44
        // Get the blocks from the inner cache, if available
45
        if (isset(self::$blockCache[$configurationPostID])) {
46
            $blocks = self::$blockCache[$configurationPostID];
47
        } else {
48
            $blocks = \parse_blocks($configurationPost->post_content);
49
            self::$blockCache[$configurationPostID] = $blocks;
50
        }
51
52
        return $blocks;
53
    }
54
55
    /**
56
     * Read the configuration post, and extract the configuration, contained through the specified block
57
     *
58
     * @param WP_Post|int $configurationPostOrID
59
     * @return array<array> A list of block data, each as an array
60
     */
61
    public static function getBlocksOfTypeFromCustomPost(
62
        $configurationPostOrID,
63
        AbstractBlock $block
64
    ): array {
65
        $blocks = self::getBlocksFromCustomPost($configurationPostOrID);
66
67
        // Obtain the blocks for the provided block type
68
        $blockFullName = $block->getBlockFullName();
69
        return array_values(array_filter(
70
            $blocks,
71
            function ($block) use ($blockFullName) {
72
                return $block['blockName'] == $blockFullName;
73
            }
74
        ));
75
    }
76
77
    /**
78
     * Read the single block of a certain type, contained in the post.
79
     * If there are more than 1, or none, return null
80
     *
81
     * @param WP_Post|int $configurationPostOrID
82
     * @return array<string, mixed>|null Data inside the block is saved as key (string) => value
83
     */
84
    public static function getSingleBlockOfTypeFromCustomPost(
85
        $configurationPostOrID,
86
        AbstractBlock $block
87
    ): ?array {
88
        $blocks = self::getBlocksOfTypeFromCustomPost($configurationPostOrID, $block);
89
        if (count($blocks) != 1) {
90
            return null;
91
        }
92
        return $blocks[0];
93
    }
94
}
95