Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality improvements #408

Merged
merged 41 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bef49fc
Format to PSR2
caendesilva Aug 11, 2022
012948c
Annotate string type as multi choice is not enabled
caendesilva Aug 11, 2022
204d9fd
Annotate string type as multi choice is not enabled
caendesilva Aug 11, 2022
d119580
Convert concatenation to scalar value
caendesilva Aug 11, 2022
7380b83
Convert concatenation to 'sprintf()' call
caendesilva Aug 11, 2022
3790544
Merge newline method call into string
caendesilva Aug 11, 2022
1fb5f7c
HydeRC is not really experimental anymore
caendesilva Aug 11, 2022
0d92608
Non-static method should not be called statically
caendesilva Aug 11, 2022
775d8b3
Add additional type check to confirm key is not null
caendesilva Aug 11, 2022
a9779c7
Change return type false to bool as method may return true
caendesilva Aug 11, 2022
c7fd712
Replace implementation type hint with contracted interface
caendesilva Aug 11, 2022
b4ab0fc
Remove duplicate parameter in comment
caendesilva Aug 11, 2022
ae796cf
Change typed property from Collection to Arrayable
caendesilva Aug 11, 2022
26599df
Apply fixes from StyleCI
StyleCIBot Aug 11, 2022
ab1ae3c
Disable PHPStorm inspection for namespaces needed in output
caendesilva Aug 11, 2022
78069c0
Replace conditional with helper
caendesilva Aug 11, 2022
4d8ea5c
Link to the test that covers the class
caendesilva Aug 11, 2022
0f6f2aa
Remove unnecessary type check as all pages have front matter support
caendesilva Aug 11, 2022
b5923b3
Apply fixes from StyleCI
StyleCIBot Aug 11, 2022
a505e4c
Extract complex code to helper methods
caendesilva Aug 11, 2022
1fc70e5
Merge branch 'master' into code-quality
caendesilva Aug 11, 2022
01d554d
Merge branch 'code-quality' of github.com:hydephp/develop into code-q…
caendesilva Aug 11, 2022
2bb7405
Add more schema documentation
caendesilva Aug 11, 2022
255c5d0
Add package annotations for schemas
caendesilva Aug 11, 2022
e85cb3d
Change return annotation to psalm-return
caendesilva Aug 11, 2022
6c747ec
Conditional does not need to be nested
caendesilva Aug 11, 2022
a4d1708
Update customization.md
caendesilva Aug 11, 2022
a4357cc
Update customization.md
caendesilva Aug 11, 2022
89b9845
Simplify if with default return value
caendesilva Aug 11, 2022
88a14c9
The route key should be used in the config
caendesilva Aug 11, 2022
709cc64
Update RELEASE_NOTES.md
caendesilva Aug 11, 2022
406b9b3
Add configuration stubs from the documentation
caendesilva Aug 11, 2022
769ee8b
Synchronize the configuration files
caendesilva Aug 11, 2022
fd6af63
Add section "Front Matter or Configuration Files?"
caendesilva Aug 11, 2022
5d9c193
Replace default value with null coalesce
caendesilva Aug 11, 2022
b8e8a85
Update RELEASE_NOTES.md
caendesilva Aug 11, 2022
aab747a
Apply fixes from StyleCI
StyleCIBot Aug 11, 2022
1b5b72c
Use getter to get canonical url
caendesilva Aug 11, 2022
73bc868
Merge branch 'code-quality' of github.com:hydephp/develop into code-q…
caendesilva Aug 11, 2022
1866476
Revert "Conditional does not need to be nested"
caendesilva Aug 11, 2022
e906909
Add PHPDocs
caendesilva Aug 11, 2022
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
4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The RoutingService class remains for compatibility with existing code, but now o
- An exception is now thrown when attempting to get the path to an Image without a defined source path or URI
- internal: The HydeKernel is now stored as a singleton within the kernel class, instead of the service container
- internal: Refactor commands with shared code to extend new abstract base class
- internal: A large part of the codebase has been refactored and cleaned up while making an effort to maintain compatibility with existing code

### Deprecated
- Deprecated interface RoutingServiceContract
Expand All @@ -49,7 +50,7 @@ The RoutingService class remains for compatibility with existing code, but now o

Prior to this release, the navigation menu priorities were based on the page slug. This has been changed to the route key. A route key in Hyde is in short the compiled page's path, relative to the site's root. For example, `_site/foo/bar.html` has the route key `foo/bar`.

This change is breaking as it requires the configuration to be updated. However, this is really easy. Just change `docs` to `docs/index` in the `config/hyde.php` file.
This change is breaking as the order of navigation items may be changed unless the configuration is updated. However, this is really easy. Just change `docs` to `docs/index` in the `config/hyde.php` file.

```diff
'navigation' => [
Expand All @@ -63,3 +64,4 @@ This change is breaking as it requires the configuration to be updated. However,
```

If you have used the config to hide the documentation page from the navigation menu, you also need to use the route key by changing `'exclude' => ['docs']` to `'exclude' => ['docs/index']`.
The same goes if you have used the config to change the navigation titles for the home and documentation pages.
6 changes: 6 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
'docs/index' => 100,
],

// In case you want to customize the labels for the menu items, you can do so here.
'labels' => [
// 'index' => 'Start',
// 'docs/index' => 'Documentation',
],

// These are the pages that should not show up in the navigation menu.
'exclude' => [
'404',
Expand Down
16 changes: 12 additions & 4 deletions docs/digging-deeper/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ When referencing configuration options, we often use "dot notation" to specify t

If you want to reference these configuration options in your Blade views, or other integrations, please take a look at the [Laravel Documentation](https://laravel.com/docs/9.x/configuration).

### Front Matter or Configuration Files?

In some cases, the same options can be set in the front matter of a page or in a configuration file. Both ways are always documented, and it's up to you to choose which one you prefer. Note that in most cases, if a setting is set in both the front matter and the configuration file, the front matter setting will take precedence.

## Configuration Files Overview

There are a few configuration files available in the `config` directory. All options are documented, so feel free to look through the files and get familiar with the options available to you.
Expand Down Expand Up @@ -186,7 +190,7 @@ Hyde makes an effort to organize the menu items in a sensible way. Putting your
'order' => [
'index' => 0, // _pages/index.md (or .blade.php)
'posts' => 10, // _pages/posts.md (or .blade.php)
'docs' => 100, // _docs/index.md
'docs/index' => 100, // _docs/index.md
]
]
```
Expand Down Expand Up @@ -226,7 +230,9 @@ Simplified, this will then be rendered as follows:

#### Excluding Items (Blacklist)

Sometimes, especially if you have a lot of pages, you may want to prevent links from showing up in the main navigation menu. To remove items from being automatically added, simply add the slug to the blacklist. As you can see, the `404` page has already been filled in for you. Note that we don't specify the page type, since only top level pages are added to the navigation menu.
Sometimes, especially if you have a lot of pages, you may want to prevent links from showing up in the main navigation menu. To remove items from being automatically added, simply add the slug to the blacklist. As you can see, the `404` page has already been filled in for you.

Note that we don't specify the page type, since only top level pages are added to the navigation menu (with the exception of the automatic documentation page link, which can be hidden in the config by using `docs/index`).

```php
'navigation' => [
Expand All @@ -249,18 +255,20 @@ navigation:

Hyde makes a few attempts to find a suitable label for the navigation menu items to automatically create helpful titles. You can override the title using the `navigation.title` front matter property.

From the Hyde config you can also override the title of the documentation label and home page link using the following options:
From the Hyde config you can also override the title of navigation links using the by mapping the slug (relative to the site root) to a title. Note that the front matter property will take precedence over the config property.


```php
// filepath config/hyde.php
'navigation' => [
'labels' => [
'docs' => 'Documentation',
'index' => 'Start',
'docs/index' => 'Documentation',
]
]
```


## Blade Views
Hyde uses the Laravel templating system called Blade. Most parts have been extracted into components to be customized easily.

Expand Down
9 changes: 8 additions & 1 deletion packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,17 @@
'navigation' => [
// This configuration sets the priorities used to determine the order of the menu.
// The default values have been added below for reference and easy editing.
// The array key should match the page's route key (slug).
'order' => [
'index' => 0,
'posts' => 10,
'docs' => 100,
'docs/index' => 100,
],

// In case you want to customize the labels for the menu items, you can do so here.
'labels' => [
// 'index' => 'Start',
// 'docs/index' => 'Documentation',
],

// These are the pages that should not show up in the navigation menu.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Hyde\Framework\Actions\Constructors;

use Hyde\Framework\Contracts\AbstractMarkdownPage;
use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use JetBrains\PhpStorm\ArrayShape;

/**
* Finds the appropriate navigation data for a page.
*
* @see \Hyde\Framework\Testing\Feature\AbstractPageTest
*/
class FindsNavigationDataForPage
{
#[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
Expand All @@ -30,6 +34,9 @@ protected function getData(): array
];
}

/**
* Note that this also affects the documentation sidebar titles.
*/
protected function getNavigationMenuTitle(): string
{
if ($this->page->matter('navigation.title') !== null) {
Expand All @@ -44,11 +51,7 @@ protected function getNavigationMenuTitle(): string
return config('hyde.navigation.labels.home', 'Home');
}

if ($this->page->matter('title') !== null) {
return $this->page->matter('title');
}

return $this->page->title;
return $this->page->matter('title') ?? $this->page->title;
}

protected function getNavigationMenuVisible(): bool
Expand All @@ -61,10 +64,8 @@ protected function getNavigationMenuVisible(): bool
return $this->page->identifier === 'index' && ! in_array($this->page->routeKey, config('hyde.navigation.exclude', []));
}

if ($this->page instanceof AbstractMarkdownPage) {
if ($this->page->matter('navigation.hidden', false)) {
return false;
}
if ($this->page->matter('navigation.hidden', false)) {
return false;
}

if (in_array($this->page->identifier, config('hyde.navigation.exclude', ['404']))) {
Expand All @@ -76,10 +77,8 @@ protected function getNavigationMenuVisible(): bool

protected function getNavigationMenuPriority(): int
{
if ($this->page instanceof AbstractMarkdownPage) {
if ($this->page->matter('navigation.priority') !== null) {
return $this->page->matter('navigation.priority');
}
if ($this->page->matter('navigation.priority') !== null) {
return $this->page->matter('navigation.priority');
}

if (array_key_exists($this->page->routeKey, config('hyde.navigation.order', []))) {
Expand Down
6 changes: 4 additions & 2 deletions packages/framework/src/Commands/HydeBuildSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public function handle(): int
if (config('docs.create_search_page', true)) {
$this->action('Generating search page', function () {
file_put_contents(
Hyde::path(sprintf('_site/%s/search.html',
Hyde::path(sprintf(
'_site/%s/search.html',
config('docs.output_directory', 'docs')
)),
view('hyde::pages.documentation-search')->render()
);
}, sprintf('Created <info>_site/%s/search.html</info>',
}, sprintf(
'Created <info>_site/%s/search.html</info>',
config('docs.output_directory', 'docs')
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function handle(): int

protected function promptForHomepage(): string
{
/** @var string $choice */
$choice = $this->choice(
'Which homepage do you want to publish?',
$this->formatPublishableChoices(),
Expand Down
9 changes: 6 additions & 3 deletions packages/framework/src/Commands/HydePublishViewsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ protected function publishOption(string $selected): void

$to = (PublishesHydeViews::$options[$selected]['destination']);

$this->line('<info>Copied</info> ['."<comment>$from</comment>".'] <info>to</info> ['."<comment>$to</comment>".']');
$this->line("<info>Copied</info> [<comment>$from</comment>] <info>to</info> [<comment>$to</comment>]");
}

protected function promptForCategory(): string
{
/** @var string $choice */
$choice = $this->choice(
'Which category do you want to publish?',
$this->formatPublishableChoices(),
Expand All @@ -54,8 +55,10 @@ protected function promptForCategory(): string

$choice = $this->parseChoiceIntoKey($choice);

$this->line('<info>Selected category</info> [<comment>'.(empty($choice) ? 'all' : $choice).'</comment>]');
$this->newLine();
$this->line(sprintf(
"<info>Selected category</info> [<comment>%s</comment>]\n",
empty($choice) ? 'all' : $choice
));

return $choice;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/framework/src/Commands/HydeServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class HydeServeCommand extends Command
*/
public function handle(): int
{
$this->line('<info>Starting the server...</info> Press Ctrl+C to stop');

$this->warn('Running experimental HydeRC 2.0. Please report any issues on GitHub.');
$this->line('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop');

$port = $this->option('port');
$host = $this->option('host');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,32 @@ trait PageSchema
* The title of the page used in the HTML <title> tag, among others.
*
* @example "Home", "About", "Blog Feed"
* @yamlType string|optional
*/
public string $title;

/**
* The settings for how the page should be presented in the navigation menu.
* All array values are optional, as long as the array is not empty.
*
* @yamlType array|optional
*
* @example ```yaml
* navigation:
* title: "Home"
* hidden: true
* priority: 1
* ```
*/
#[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
public ?array $navigation = null;

/**
* The canonical URL of the page.
*
* @var string|null
* @yamlType array|optional
*
* @example "https://example.com/about"
*/
public ?string $canonicalUrl = null;

Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Contracts/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function getOutputLocation(string $basename): string
/** @inheritDoc */
public function get(string $key = null, mixed $default = null): mixed
{
if (property_exists($this, $key) && isset($this->$key)) {
if ($key !== null && property_exists($this, $key) && isset($this->$key)) {
return $this->$key;
}

Expand Down Expand Up @@ -145,7 +145,7 @@ public function getSourcePath(): string
/** @inheritDoc */
public function getOutputPath(): string
{
return static::getCurrentPagePath().'.html';
return $this->getCurrentPagePath().'.html';
}

/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Models/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function generate(): void
}

if ($this->page->has('canonicalUrl')) {
$this->add(Meta::link('canonical', $this->page->canonicalUrl));
$this->add(Meta::link('canonical', $this->page->get('canonicalUrl')));
}

if ($this->page->has('title')) {
Expand Down
4 changes: 1 addition & 3 deletions packages/framework/src/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ protected function compilePagesForClass(string $pageClass): void
$this->newLine(2);
}

/**
* @return \Closure(Route):string
*/
/** @psalm-return \Closure(Route):string */
protected function compileRoute(): \Closure
{
return function (Route $route) {
Expand Down
46 changes: 28 additions & 18 deletions packages/framework/src/Services/MarkdownService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,15 @@ protected function setupConverter(): void
// Determine what dynamic extensions to enable

if ($this->canEnablePermalinks()) {
$this->addExtension(HeadingPermalinkExtension::class);

$this->config = array_merge([
'heading_permalink' =>[
'id_prefix' => '',
'fragment_prefix' => '',
'symbol' => '#',
'insert' => 'after',
'min_heading_level' => 2,
],
], $this->config);
$this->configurePermalinksExtension();
}

if ($this->canEnableTorchlight()) {
$this->addExtension(TorchlightExtension::class);
}

if (config('markdown.allow_html', false)) {
$this->addExtension(DisallowedRawHtmlExtension::class);

$this->config = array_merge([
'disallowed_raw_html' => [
'disallowed_tags' => [],
],
], $this->config);
$this->enableAllHtmlElements();
}

// Add any custom extensions defined in config
Expand Down Expand Up @@ -217,4 +201,30 @@ protected function injectTorchlightAttribution(): string
'Syntax highlighted by torchlight.dev'
));
}

protected function configurePermalinksExtension(): void
{
$this->addExtension(HeadingPermalinkExtension::class);

$this->config = array_merge([
'heading_permalink' => [
'id_prefix' => '',
'fragment_prefix' => '',
'symbol' => '#',
'insert' => 'after',
'min_heading_level' => 2,
],
], $this->config);
}

protected function enableAllHtmlElements(): void
{
$this->addExtension(DisallowedRawHtmlExtension::class);

$this->config = array_merge([
'disallowed_raw_html' => [
'disallowed_tags' => [],
],
], $this->config);
}
}
Loading