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.

BlockHelpers::getBlocksFromCustomPost()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 1
dl 0
loc 28
ccs 0
cts 17
cp 0
crap 42
rs 9.1111
c 0
b 0
f 0
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 (is_null($configurationPost) || !$configurationPostID) {
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
            fn ($block) => $block['blockName'] == $blockFullName
72
        ));
73
    }
74
75
    /**
76
     * Read the single block of a certain type, contained in the post.
77
     * If there are more than 1, or none, return null
78
     *
79
     * @param WP_Post|int $configurationPostOrID
80
     * @return array<string, mixed>|null Data inside the block is saved as key (string) => value
81
     */
82
    public static function getSingleBlockOfTypeFromCustomPost(
83
        $configurationPostOrID,
84
        AbstractBlock $block
85
    ): ?array {
86
        $blocks = self::getBlocksOfTypeFromCustomPost($configurationPostOrID, $block);
87
        if (count($blocks) != 1) {
88
            return null;
89
        }
90
        return $blocks[0];
91
    }
92
}
93