What rule prevents empty lines after the class opening brace? - php

I'm looking for a rule that prevents empty lines after the opening brace of a class.
I found the rule Squiz.WhiteSpace.FunctionOpeningBraceSpace for functions, but I can't find any for classes.
Any suggestions are appreciated.

You are looking for no_blank_lines_after_class_opening from FriendsOfPHP/PHP-CS-Fixer.
If you want to use CodeSniffer, you still can. With EasyCodingStandard you can use both.
# easy-coding-standard.neon
checkers:
- PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer
Do you want more? Check this short post.

Related

SonarQube: ruleset doesn't respect PSR

I have a little issue with SonarQube to scan PHP code.
When scanning the work, there is an error coming a hundred times:
Move this open curly brace to the end of the previous line
This apply on this code for example:
class Edit extends Container
{
It shouldn't as PSR are clear on this topic :
Opening braces for classes MUST go on the next line, and closing
braces MUST go on the next line after the body.
Opening braces for
methods MUST go on the next line, and closing braces MUST go on the
next line after the body.
Opening braces for control structures MUST
go on the same line, and closing braces MUST go on the next line after
the body
When I look in the detail of the error in Sonar, this is what the rule seems to follow, which is not appropriate:
Sharing some coding conventions is a key point to make it possible for a team to efficiently collaborate. This rule make it mandatory to place open curly braces at the end of lines of code.
And there it shows me the example of the if condition
if(...) {
//...
}
Any idea to follow the proper rule?
Finally I found a solution:
edit your project configuration by disabling the faulty rules and enabling the wanted ones, in this case: PSR2. You can also add some additionnal rules, for example in my case, the Magento 2 ones ;
to make the changes taken in to account: launch a new build.
Hope it will help other.

Best way to remove wrong tags from string

Whats the best way to remove wrong tag?
Tag is wrong if it is contained within the same tag
For example, this line
<q>+7</q> (<q><q>9</q>6</q><q>2</q>) <q>9</q><q>3</q><q>7</q> <q>4</q><q>2 2</q><q>4</q>
After remove wrong tags:
<q>7</q> (<q>96</q><q>2</q>) <q>9</q><q>3</q><q>7</q> <q>4</q><q>2 2</q><q>4</q>
More than this simple example:
Before:
<p>bla-bla <p>bla-bla</p> bla-bla</p>
After:
<p>bla-bla bla-bla bla-bla</p>
This question pretty much covers everything you need to know: RegEx match open tags except XHTML self-contained tags
The long and short of it: You don't use regex for this. You "could" but it would be so complex that it would create issues elsewhere in your code. Either trim your codebase, run an html purifier, an xml parser, or something else along those lines. Regex is likely the wrong solution for your issue.
you can use this pattern:
(<(.+)>)(.+?)(<\2>)(.*?)(<\/\2>)(.*?)(<\/\2>)$
and replace your string for
$1$3$5$7$8
I believe that you can do like this on PHP
echo preg_replace("(<(.+)>)(.+?)(<\\2>)(.*?)(<\\/\\2>)(.*?)(<\\/\\2>)$", "$1$3$5$7$8", "<p>bla-bla <p>bla-bla</p> bla-bla</p>");
you can see a live example of this regex working here http://regexr.com/3e8o3
An important remark is that you may need to call this in a while loop until the regex stop matching, because this regex is not recursive, so this will not work for 3 or more levels of nesting.

Matching all three kinds of PHP comments with a regular expression

I need to match all three types of comments that PHP might have:
# Single line comment
// Single line comment
/* Multi-line comments */
 
/**
* And all of its possible variations
*/
Something I should mention: I am doing this in order to be able to recognize if a PHP closing tag (?>) is inside a comment or not. If it is then ignore it, and if not then make it count as one. This is going to be used inside an XML document in order to improve Sublime Text's recognition of the closing tag (because it's driving me nuts!). I tried to achieve this a couple of hours, but I wasn't able. How can I translate for it to work with XML?
So if you could also include the if-then-else login I would really appreciate it. BTW, I really need it to be in pure regular expression expression, no language features or anything. :)
Like Eicon reminded me, I need all of them to be able to match at the start of the line, or at the end of a piece of code, so I also need the following with all of them:
<?php
echo 'something'; # this is a comment
?>
Parsing a programming language seems too much for regexes to do. You should probably look for a PHP parser.
But these would be the regexes you are looking for. I assume for all of them that you use the DOTALL or SINGLELINE option (although the first two would work without it as well):
~#[^\r\n]*~
~//[^\r\n]*~
~/\*.*?\*/~s
Note that any of these will cause problems, if the comment-delimiting characters appear in a string or somewhere else, where they do not actually open a comment.
You can also combine all of these into one regex:
~(?:#|//)[^\r\n]*|/\*.*?\*/~s
If you use some tool or language that does not require delimiters (like Java or C#), remove those ~. In this case you will also have to apply the DOTALL option differently. But without knowing where you are going to use this, I cannot tell you how.
If you cannot/do not want to set the DOTALL option, this would be equivalent (I also left out the delimiters to give an example):
(?:#|//)[^\r\n]*|/\*[\s\S]*?\*/
See here for a working demo.
Now if you also want to capture the contents of the comments in a group, then you could do this
(?|(?:#|//)([^\r\n]*)|/\*([\s\S]*?)\*/)
Regardless of the type of comment, the comments content (without the syntax delimiters) will be found in capture 1.
Another working demo.
Single-line comments
singleLineComment = /'[^']*'|"[^"]*"|((?:#|\/\/).*$)/gm
With this regex you have to replace (or remove) everything that was captured by ((?:#|\/\/).*$). This regex will ignore contents of strings that would look like comments (e.g. $x = "You are the #1"; or $y = "You can start comments with // or # in PHP, but I'm a code string";)
Multiline comments
multilineComment = /^\s*\/\*\*?[^!][.\s\t\S\n\r]*?\*\//gm

Showing a literal "#link" in phpDoc

I'm writing a phpDocumentor mini-tutorial for a lightning talk, and I'm using, of course, phpdoc to generate examples. When it comes to information on how to use inline #link tags though, I haven't found a way that works, yet. I've tried both the html entity for # (#) and escaping with a backslash among other things, but to no avail. Does anyone know the magic syntax to escape #link?
Use an extra closing brace immediately following the # character... from the manual [1]:
If I want to use the characters "{#link" in a docblock, I just use "{#}link."
[1] -- http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_inlinetags.pkg.html

Debugging PHP Output

I have a php website that on certain pages is adding a dot or space before the first html tag. I can't figure out where it is coming from - is there a way to debug the code so i can see where it is coming from?
Thanks,
Josh
To help prevents this happening it is considered a good practice to don't end your PHP file with a ?>.
You possibly have some file that are this way (notice the extra space after the ?>):
<?php
// Some code //
?>
If you would remove the ?> at the end, the extra space at the end of the file won't be interpreted as something to output.
For files that contain only PHP code,
the closing tag ("?>") is never
permitted. It is not required by PHP,
and omitting it´ prevents the
accidental injection of trailing white
space into the response.
Source: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html
Maybe it is a BOM character?
Maybe you should check your templates if you are using them... the problem could be there and not in your main code.
and yes is a GOOD PRACTICE in PHP not to close the ending tag.
There really is no good way to go about debugging this. You need to go through every file the page is hitting and figure out where the output is coming from. If you really wanted to be lazy about it you could do some output buffering, but this isn't the right way to do things.
Problems like this can be difficult to track down. If you're in some kind of framework or system that includes a lot of files, you might try a var_dump(get_included_files()) on the line before your error occurs, and that will give you a place to start. If that isn't sufficient, xdebug might get you further. Things to look out for are space before and after the PHP tags, and functions that might send output.

Categories