PhpStorm does not reformat the complete code properly - php

Since I have many (bad coded/indented) files, that look like this:
After doing a reformat, most code is indented right, but some parts still look like this:
After having a closer look (with sublime) I noticed that these lines are not indented with tabs, but with spaces:
So why are those parts not indented right with tabs?
Additional question: How can I remove the empty lines for the hole project?

The issue with spaces instead of tabs and original indentation -- you need to remove span from the "Keep white spaces inside" list in Code Style for HTML.
Empty lines -- most of them you can get rid if you disable "Keep line breaks" sort of options and put 0 into "Maximum black lines" kind of fields -- there quite a few of them in each language (HTML & PHP in your case).
Unfortunately those options do not seem to have any effect on empty lines between PHP code blocks (between '?> and <?php). For such cases I suggest use regex and replace \n\n (2 line endings in a row) by single \n (you may need to run such replacement few times if you have lots of consecutive empty lines).

Related

Atom beautify weird linebreaks

I have a weird bug. When I beautify my html/php mixed code it breaks the line and puts the echo and the following tag on 2 different lines making it look ugly.
Anyone had this problem before ?
BTW look at $answer_hash['topic_firstname'] suddenly the following )) are green/commented looks like.
Not sure, my only guess would be that you have softwrap (also called wordwrap) on. You can disabled it at: View -> Toggle softwrap.
To awnser your second question, the value="" attribute normal contains string, and not code. So it gets the green color as if it is a string.
You're using the standard syntax theme that doesn't give a color to parentesis and curly braces, so since it's in a value="" attribute it gets the green color.
It's default behavior of PHP-CS-Fixer (I assume you use the default PHP beautifier of atom-beautify in version 2) to break lines after curly braces.
The body of each structure MUST be enclosed by braces. Braces should
be properly placed. Body of braces should be properly indented.
Scroll down to 'braces [#PSR2, #Symfony]'
Which is defined in the PSR-2 coding style guide:
Opening braces for control structures MUST go on the same line, and closing
braces MUST go on the next line after the body.
If you switch back to version 1 in the atom-beautify settings it won't do the line breaks but it stop fixing other things only available in version 2.
For completeness (although rpm192) has already answered the second part of your question: The )) are green because the standard color for html attribute values is green. Since your document type is set to PHP, all built in functions, arrays etc. between PHP-tags are colored differently but everything else, which does not have a different color specified, keeps it's previous color (which in this case is green). As you can see in the screenshot if you place the PHP-code outside of the html attribute value the braces are colored white as usual.
Note: You have more opening curly braces { than closing ones. Check out line 6 in my screenshot.

Targeting specific PHP tag with regex

All my wordpress websites have recently been hacked, and a very long PHP line has been added on top of all PHP files.
It looks like that (juste a sample of the entire code)
<?php $gqmtlkp = '~ x24<!%o:!>! x242178}527}88:}35csboe))1/35.)1/14+9**-)1/2986+7452]88]5]48]32M3]317]445]212]445]43]321]y]252]18y]#>q%
The problem is that code is generated and is different in all files. But I noticed that every code contains
explode(chr((729-609))
Can someone help me with building a regex line, that will target first php tag (optional) containing : (numbers vary)
explode(chr((xxx-xxx))
so that I can automatically remove it in every files ?
Thanks a lot for your help
Based on my understanding of your request you're looking to escape the following format: <?php(optional) explode(chr((xxx-xxx))) <- your sample was missing a third closing paranthesis for explode() function so I added it. If that's not right then just remove the last \) portion.
Try this: /(\<\?php)? explode\(chr\(\([0-9]{3,3}-[0-9]{3,3}\)\)\)/
Not sure if space after optional first php tag is necessary. You can adjust it going from there.

replacing single break tags for two using regex

Whilst being aware of the pitfalls/dangers of certain html manipulation with regex (instead of using say the PHP dom manipulator) I'm trying to achieve something that should be pretty simple and not that risky.
Basically I have some uncleaned html copy from a database that doesn't use paragraphs but line break tags to produce the effects of paragraphs. Sometimes though the user only entered content with a single break so that the text line returns but without a blank line appearing. In such instances and ONLY in such instances I want to replace that single <br> with two (<br><br>).
So as an example...
This is <br>a test<br><br>example!
would become
This is <br><br>a test<br><br>example!
Note how the second set of breaks is left alone as its already got 2 tags.
Simply replace one or more occurences of <br> with <br> :)
Replace what:
(<br>)+
Replace with:
<br><br>
You can use negative lookahead and lookbehind to solve this:
(?<!<br>)<br>(?!<br>)
See the example here: http://rubular.com/r/WYjoenH1SA
(?<!NOPREFIX)
(?!NOPOSTFIX)
The first part prevents from matching, if the NOPREFIX is present - the second one if NOPOSTFIX is present.

PHP eats linefeed in php/plaintext mixed mode

This one is bothering me for a while. Let's say we have a simple PHP-File:
Line 0
Line 1
<?="Line 2"?>
Line 3
Processing this file will result in:
Line 0
Line 1
Line 2Line 3
Where did the line feed after ?> go? The linefeed is not beeing devoured when placing some character after the closing tag (e.g. ?>.).
Is there a way to control this behaviour? I'm not willing to place whitespaces after the closing tag, because my IDE is configured to remove whitespaces before linefeeds (and I like it that way).
Yes, indeed:
The closing tag for the block will include the immediately trailing newline if one is present.
http://php.net/manual/en/language.basic-syntax.instruction-separation.php
Meaning, if the ?> is the last thing on the line, the newline will be removed as part of the closing PHP block. You need to explicitly echo a newline or add an additional newline.
This is actually a feature (believe it or not). PHP consumes a linefeed if it directly follows a PHP close tag:
The closing tag for the block will include the immediately trailing
newline if one is present.
This was clearly put in so that a PHP file ending with a blank line would not cause a newline to occur in the output when included from another script. So it's really a "protect the ignorant" feature from the old days that we have to live with for the foreseeable future.
If you really want the newline there are other options: from simply putting in two newlines after the closing tag (the second will work!) to echoing a newline from code.
Outside of the <?php and ?> tags, the PHP interpreter operates in HTML mode and spacing inside HTML mode is less of an issue than it is for text contents.
To generate text with PHP you should use plain strings and build your output in this fashion:
$var = "Line 2";
$s = "Line 0\nLine 1\n$var\nLine3";
At least this won't give you a nasty, though documented, surprise :)

Have I misunderstood what heredoc should do?

I'm very new to PHP so I know I am missing something obvious here -
I thought the heredoc function is supposed to retain formatting, line breaks, etc.
But whenever I test it, while it parses, there is no formatting.
I've tried lots of different scripts including copy-and-pasts from sources such as PHP.net and W3schools - so I know there is nothing wrong with the scripts. Can't google up an answer to this one - probably because it's too obvious?? (BTW, testing with MAMP). Thanks.
HEREDOC is not a function, it is a method for specifying string delimiters that allows you to forgo escaping quotes within the heredoc (I like it because good text editors will syntax highlight their contents based on the heredoc name).
HEREDOCs do preserve all whitespace, newlines, etc. I think you are probably looking at the results in a browser. Browsers generally trim all whitespace, newlines, spaces, and tabs included, down to a single space. Try looking at it on the command line, a text email, or a text file.
It will produce a string identical to the one you set.
However, browsers render multiple whitespace condensed to one space character. This is by design.
To preserve your spaces, you can use the pre element (assuming default browser stylesheet) or white-space: pre CSS property.
<div style="white-space: pre">
<?php echo <<<A
some text
preserved
just
how
you want it.
A; ?>
</div>
jsFiddle.
Because you said "testing with MAMP" I assume, that you use your webbrowser to display the content. Thats a slightly wrong approach, because the webbrowser itself strips down any unnecessary whitespaces. Look at the source of the page you display in your browser and you will see the "real" content.
It does, but only for what it outputs.
If it outputs HTML, then that HTML is going to be interpreted by a browser using the normal rules for handling whitespace.

Categories