Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Services/ThemeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ public function sectionsWithAppBlock(array $templateMainSections): array
$asset = $assetResponse['body']['asset']->toArray();

preg_match('/\{\%\s+schema\s+\%\}([\s\S]*?)\{\%\s+endschema\s+\%\}/m', $asset['value'], $matches);
$schema = json_decode($matches[1], true);

if ($schema && isset($schema['blocks'])) {
$acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type'));
if (!empty($matches) && isset($matches[1])) {
$schema = json_decode($matches[1], true);
if ($schema && isset($schema['blocks'])) {
$acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type'));
}
}
Comment on lines +211 to 216
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !empty($matches) is redundant. When preg_match fails to find a match, $matches will be an empty array, so isset($matches[1]) alone is sufficient to check if the first capture group exists.

Copilot uses AI. Check for mistakes.


return $acceptsAppBlock ? $file : null;
Expand Down
28 changes: 28 additions & 0 deletions tests/Services/ThemeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,32 @@ public function testSectionsWithAppBlock(): void

$this->assertNotEmpty($sectionsWithApp);
}

public function testSectionsWithAppBlockHandlesMissingSchema(): void
{
// Response stubbing
$this->setApiStub();
ApiStub::stubResponses([
'get_themes',
'get_theme_assets',
'get_theme_asset_no_schema',
]);

// Define config to include a template
$this->app['config']->set(
'shopify-app.theme_support.templates',
[
'asset',
]
);

// Run extraction
$this->helper->extractStoreMainTheme($this->shop->getId());
$jsonFiles = $this->helper->templateJSONFiles();
$mainSections = $this->helper->mainSections($jsonFiles);
$sectionsWithApp = $this->helper->sectionsWithAppBlock($mainSections);

// Assert no sections are returned and no error occurs
$this->assertEmpty($sectionsWithApp);
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/get_theme_asset_no_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"asset": {
"key": "sections/no-schema.liquid",
"value": "<!-- Invalid schema -->"
}
}
Loading