How to exclude specific rule in a custom phpcs.xml ruleset? - coding-style

I am looking for a way to automate coding standards and I've decided to use SlevomatCodingStandard.TypeHints.TypeHintDeclaration in our phpcs.xml file. Unfortunately we have faced with an issue with the error reporting:
38 | ERROR | [ ] #var annotation of property
| | \VC4A\ConvertUploadsToS3::$missing_local_files
| | does not specify type hint for its items.
45 | ERROR | [ ] #var annotation of property
| | \VC4A\ConvertUploadsToS3::$missing_s3_files does
| | not specify type hint for its items.
52 | ERROR | [ ] #var annotation of property
| | \VC4A\ConvertUploadsToS3::$updated_s3_links does
| | not specify type hint for its items.
66 | ERROR | [ ] #var annotation of property
| | \VC4A\ConvertUploadsToS3::$s3_settings does not
| | specify type hint for its items.
112 | ERROR | [ ] Method \VC4A\ConvertUploadsToS3::setup_crons()
| | does not have return type hint nor #return
| | annotation for its return value.
149 | ERROR | [ ] #return annotation of method
| | \VC4A\ConvertUploadsToS3::get_cron_actions() does
| | not specify type hint for items of its traversable
| | return value.
202 | ERROR | [ ] #return annotation of method
| | \VC4A\ConvertUploadsToS3::get_cron_intervals()
| | does not specify type hint for items of its
| | traversable return value.
266 | ERROR | [ ] #param annotation of method
| | \VC4A\ConvertUploadsToS3::error_reporting() does
| | not specify type hint for items of its traversable
| | parameter $uploads.
334 | ERROR | [ ] #param annotation of method
| | \VC4A\ConvertUploadsToS3::update_s3_link() does
| | not specify type hint for items of its traversable
| | parameter $upload.
336 | ERROR | [x] Method \VC4A\ConvertUploadsToS3::update_s3_link()
| | does not have void return type hint.
I want to add strict return types for methods only but for the DocBlocks we are using WordPress-Docs standards.
Is there a way to disable the rest of the errors except the last one ?
What I've tried was this:
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration">
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.enableEachParameterAndReturnInspection"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.normalizedTraversableTypeHints"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.normalizedUsefulAnnotations"/>
</rule>
Apparently this does not have any effect. Any suggestions ? Or what could be the proper way to add that specific rule only ?
Or is it possible to do it other way around ? Just add a specific rule in a ruleset without modifying the codebase ?

Apparently, I should have used the constant values instead of class data member names:
<!-- Strict type return for methods -->
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration">
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
</rule>
Source:
https://github.com/slevomat/coding-standard/issues/570#issuecomment-448647567

Related

Laravel | Return rows in order based off pivot

I have the following setup:
stages:
| id | name | order |
commands:
| id | name | body |
------------------------------=
| 1 | test | phpunit |
| 2 | style | echo "style" |
| 3 | deploy | deploy |
command_stage:
| command_id | stage_id | order |
---------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
Basically, I would like to create a method on the stage model which allows me to get all of the commands back based off of the order, but commands have a specific order and so do the stages.
So each command is stored under a stage so we know which part to run but each command also has an order in that stage. Now I know I can do something like the following:
$inOrder = collect();
Stage::get()->each(function ($stage) {
$commands = $stage->commands()->orderByPivot('order')->get();
$inOrder->push($commands);
});
return $inOrder;
But I was wondering if there is a nicer way to do this? Or even a way to do this solely on one database hit?
Pre-load and sort the relationship:
$stages = Stage::with(['commands' => function ($subQuery) {
$subQuery->orderBy('command_stage', 'order');
}])->get();
foreach($stages as $stage) {
$inOrder->push($stage->commands);
}
This method of Eager-Loading will reduce the N+1 query issue of doing $stage->commands() inside the foreach() loop.

Index Route missing in Resource Routes Laravel

I am unable to get the index route of my resource when using some prefix,
when my route is this:
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
i got this route listing:
| subjects | subjects.index
| subjects | subjects.store
| subjects/create | subjects.create
| subjects/{subject} | subjects.update
| subjects/{subject} | subjects.destroy
| subjects/{subject} | subjects.show
| subjects/{subject}/edit | subjects.edit
But when i add a prefix like this:
Route::prefix('admin')->group(function () {
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
});
The index route disappears, and routes list become this:
| admin/subjects | subjects.store
| admin/subjects/create | subjects.create
| admin/subjects/{subject} | subjects.update
| admin/subjects/{subject} | subjects.destroy
| admin/subjects/{subject} | subjects.show
| admin/subjects/{subject}/edit | subjects.edit
Got the solution. actually there was another route ('subjects') in my web.php. I needed to remove this, since they both were colliding.

Use a non-unique route key name?

We have rows which are sequenced and scoped to each organisation. So:
Processes tbl:
| id | org_id | sequence | name |
|---- |-------- |---------- |------------------- |
| 23 | 1 | 1 | New person |
| 56 | 1 | 2 | Person leaves |
| 84 | 1 | 3 | Person absent |
| 26 | 2 | 1 | Something happens |
| 34 | 2 | 2 | Something else |
| 77 | 2 | 3 | And another |
We do this so they can have a simple numbering system for their own records rather than a UUID, hash or something else. We prefer to have this in the url as well, so:
example.com/processes/1
Once logged in, we know their org_id, so for each query we can use org_id along with the sequence from the url.
Is there a clean laravel-esque kind of way to achieve this, maybe using the Model's getRouteKeyName?
I got the answer with some help from the laracasts forum:
/**
* Retrieve the model for a bound value.
*
* #param mixed $value
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('sequence', $value)
->where('org_id', /* however you get your org_id */)
->first() ?? abort(404);
}

Sphinx malformed table

We've got a RST table in sphinx that looks like this:
+-------------------------+---------------------------------------------+-----------------------------------------+
| Key | Appearance in the administration | Value |
+=========================+=============================================+=========================================+
| |text_line| | simple text input | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |text_area| | text area | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |text_editor| | text editor with formatting capabilities | HTML string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |checkbox| | checkbox | boolean |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |single_select| | list of radio buttons | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |multiple_select| | list of checkboxes | array of strings |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |color| | color picker | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |date| | date picker | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |time| | text input with time validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |url| | text input with URL validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |email| | text input with email validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |password| | password input | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |phone| | text input for a phone number | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |internal_links| | widget for selecting links to other pages | resolved pages as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |single_internal_link| | widget for selecting a single page | resolved page as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |smart_content| | widget for configuring a data source | resolved pages as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |resource_locator| | widget for entering the URL of a page | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |tag_list| | autocomplete input for entering and adding | array of strings |
| | tags | |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |category_list| | autocomplete input for entering and adding | array of strings |
| | tags | |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |media_selection| | widget for selecting media (images, | array containing arrays with |
| | documents) | urls for every format |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |contact_selection| | widget for selecting contacts | array containing array representations | |
| | | of the contact objects |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |snippet| | widget for selecting snippets | array containing array representations |
| | | of the snippets |
+-------------------------+---------------------------------------------+-----------------------------------------+
Since recently I get the following error when trying to build the documentation using make build:
ERROR: Malformed table.
It also outputs this table again, but without any hint what is wrong here, and I can't find it. Can anybody help me out?
While trying to format the code correctly I found the error on my own... The row for contact_selection contained a pipe at the very end of the line, after some tabs...

How can I compare against FileSystemRights using Powershell?

I want to check whether a given user has access to a given folder - by checking if they have "Modify" access assigned to them.
I thought that the PS for that would be:
(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} |?{$_.filesystemrights.value -contains "Modify"}
But the final part of that isn't working - I get back no result. But I know that they have Modify access - if I put in:
(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} | select -ExpandProperty filesystemrights
then I get back:
Modify, Synchronize
ReadAndExecute, Synchronize
Is this because the FileSystemRights property is an enumeration? And if so, how do I test against it?
It's a type problem. (Get-Acl .\myfolder).Access[].FileSystemRights is of type System.Security.AccessControl.FileSystemRights. It's not really displaying a string. To make it a string, just use the ToString() method:
(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} |?{$_.filesystemrights.ToString() -contains "Modify"}
Or you can use the bitwise comparison method. However, it's very easy to confuse when you want to use this:
($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Modify) -eq [System.Security.AccessControl.FileSystemRights]::Modify
With when you want to use this:
($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Modify) -eq $_.FileSystemRights
They have very different meanings. For example, if you have Full Control, the former test is still true. Is that what you want? Or do you want to know when the FileSystemRights are literally just Modify?
Also, [System.Security.AccessControl.FileSystemRights] is an incomplete enumeration. In my environment, I found I needed this table:
+-------------+------------------------------+------------------------------+
| Value | Name | Alias |
+-------------+------------------------------+------------------------------+
| -2147483648 | GENERIC_READ | GENERIC_READ |
| 1 | ReadData | ListDirectory |
| 1 | ReadData | ReadData |
| 2 | CreateFiles | CreateFiles |
| 2 | CreateFiles | WriteData |
| 4 | AppendData | AppendData |
| 4 | AppendData | CreateDirectories |
| 8 | ReadExtendedAttributes | ReadExtendedAttributes |
| 16 | WriteExtendedAttributes | WriteExtendedAttributes |
| 32 | ExecuteFile | ExecuteFile |
| 32 | ExecuteFile | Traverse |
| 64 | DeleteSubdirectoriesAndFiles | DeleteSubdirectoriesAndFiles |
| 128 | ReadAttributes | ReadAttributes |
| 256 | WriteAttributes | WriteAttributes |
| 278 | Write | Write |
| 65536 | Delete | Delete |
| 131072 | ReadPermissions | ReadPermissions |
| 131209 | Read | Read |
| 131241 | ReadAndExecute | ReadAndExecute |
| 197055 | Modify | Modify |
| 262144 | ChangePermissions | ChangePermissions |
| 524288 | TakeOwnership | TakeOwnership |
| 1048576 | Synchronize | Synchronize |
| 2032127 | FullControl | FullControl |
| 268435456 | GENERIC_ALL | GENERIC_ALL |
| 536870912 | GENERIC_EXECUTE | GENERIC_EXECUTE |
| 1073741824 | GENERIC_WRITE | GENERIC_WRITE |
+-------------+------------------------------+------------------------------+
It's interesting to compare the output of these:
[System.Enum]::GetNames([System.Security.AccessControl.FileSystemRights]);
[System.Enum]::GetNames([System.Security.AccessControl.FileSystemRights]) | % { "$($_.ToString())`t`t$([System.Security.AccessControl.FileSystemRights]$_.ToString())`t`t$(([System.Security.AccessControl.FileSystemRights]$_).value__)";}
[System.Enum]::GetValues([System.Security.AccessControl.FileSystemRights]) | % { "$($_.ToString())`t`t$(($_).value__)";}
The GENERIC rights are not enumerated in the .Net class, but you will see that numeric value if you enumerate enough files.
Good luck!
Got it:
(get-acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} | ?{($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Modify) -eq [System.Security.AccessControl.FileSystemRights]::Modify}
It's both a bitwise comparison - and therefore you need to use "-band".
But "-band" will return true if any of the same bits are set in both enumerations. And as even "Read" has several bits set (it's 100000000010001001) - some of which will match with "Modify", you need to also compare the result with "Modify" to make sure that the result is actually the same.
(Thanks to the comments below for getting me pointed in the right direction.)
Updated new version.
Clarified version from Arco's comment.
With this version we're checking if the Modify bit is set.
(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} |?{ $_.FileSystemRights -band [Security.AccessControl.FileSystemRights]::Modify}
The value__ property is the numeric bit set version.

Resources