PHP Code Sniffer shows warning in PhpStorm but not in CLI - phpstorm

I have a little function in a class:
/**
* #return float|int
*/
private function getPrice()
{
return rand(1000, 1000000) / 10;
}
In PhpStorm I am getting a warning by phpcs.
Missing function's return type declaration
But when I am running the phpcs from command line by:
phpcs --standard=PSR12 ./
I am getting no error message about this return type.
I've tried to search it in ruleset, but there is only a comment about this:
<!-- When you have a return type declaration present, there MUST be one space after the colon followed by the type declaration. The colon and declaration MUST be on the same line as the argument list cl
<!-- checked by PSR12.Functions.ReturnTypeDeclaration -->
But I did not see any rule for this.
Can somebody help me, how can I turn it on from the ruleset.xml or from command line?

Missing function's return type declaration is a PhpStorm inspection; it doesn't come from PHP_CodeSniffer, which doesn't have this feature yet.

Related

Check if variable is empty in PhpStorm file template

I'm trying to follow Zend coding standard for comments blocks for functions and I've stuck during PHP Function Doc Commentcustomization.
This is my current code look
/**
${PARAM_DOC}
#if(${PARAM_DOC})
*
#end
* #return ${TYPE_HINT}
${THROWS_DOC}
*/
The assumption for this is that it should add an asterisk only if ${PARAM_DOC} is not nullable, but this code doesn't work. It always adds an asterisk. The documentation of PhpStorm Variables doesn't contain any useful informations for my problem so I hope that somebody here can help me.
My PhpStorm version is 2019.3 EAP.
As the build-in PhpStorm for ${PARAM_DOC} description says
Parameters' doc comment.
Generated as a number of lines '* #param type name". If there are no parameters, evaluates to an empty content.
And Apache Velocity docs says
When VTL references a variable, such as $foo, the variable can get its value from either a set directive in the template, or from the Java code.
After that I understood that I'm making one important mistake. PhpStorm is based on Java and an empty String cant be automatically casted to Boolean beacuse ${PARAM_DOC} is just a Java String.
So solution for that turned out to be
#if (${PARAM_DOC} != "")
*
#end
Obvious but not quiet.

::itcl::delete throws error "invalid command name"

I am using itcl delete command to delete objects and classes. However, tcl interpreter says "invalid command name "delete". Here is the partial code snippet.
% itcl::find classes
datapath point datapath_point
itcl::find objects
datapath_point0 datapath_point1 datapath0
% itcl::delete object datapath_point0
invalid command name "delete"
Thanks,
boppu
With Peter, comments I could look for the Error in my code. In one of the base class, I had the following code.
destrutor {
delete object $this
}
Here, delete namespace was missing. Even though you add "itcl::delete", you will endup in another error. Simple and correct solution should be an empty destructor.
destructor {
}

How to create new phpDoc annotations in PhpStorm 8

I searched via Google but couldn't find an answer to this. PhpStorm has many built in annotations for code completion but many are missing as well. I am pretty sure there is a way to create new annotations but couldn't find it anywhere within the settings. Or maybe it is an XML file somewhere. NetBeans has support for this feature.
In other words, how can I create new phpDoc annotations for completion in phpStorm 8 like #usesDefaultClass for phpUnit.
I have found this answer here: http://blog.jetbrains.com/webide/2013/02/phpdoc_and_code_templates/
Hope this will help you
PhpDoc Templates
PhpDoc templates are located under Settings | File Templates | Includes. Currently there are three templates named PHP Class Doc Comment, PHP Function Doc Comment and PHP Field Doc Comment. Any of these can be included into other templates via the #parse directive. For example, we can modify the original class template (Templates | PHP Class) to also include the class PHP Doc when a class is generated:
<?php
#parse("PHP File Header.php")
...
#parse("PHP Class Doc Comment")
class ${NAME} {
}
The same templates are used when:
A new PHP Doc comment is generated after we type /** and press Enter before a class, function (method) or class field.
We invoke Code | Generate | PHPDoc blocks or use the Add PHP Doc quick-fix for the inspection Missing PHP Doc.
Below is a list of variables that can be used in PHP Doc templates:
${NAME}
The element (class, function, field) name.
${NAMESPACE}
The name of the namespace the element belongs to without any leading or trailing backslashes, for example Core\Widgets. The variable value is empty if the element doesn’t belong to any namespace. A check like `#if (${NAMESPACE})` ... is possible.
${CLASS_NAME}
Contains a class name for class methods and fields. Will be empty for functions that do not belong to any class.
${TYPE_HINT}
For functions (methods), contains the return type of the function (method). For fields, evaluates to the field’s type if it can be found, for example, from a default value. Will be empty if the type cannot be retrieved from the code.
${STATIC}
Takes the value of “static” if the function or field is static, otherwise, an empty string. We can use this variable with the condition `#if (${STATIC})` ... to generate something specific for static class members.
${CARET}
Marks the position where the editor caret should be placed after the comment is added. Note: Works only if the comment is added after we type “/**” and hit Enter. Should be placed inside the comment, not on the first line /** or on the last line */. In all other cases the caret marker will be ignored.
${PARAM_DOC}
A generated PHP Doc fragment containing function (method) parameters in the form: “* #param type $name“. For example, if the function signature is foo ($x, $y), it will evaluate to:
#param $x
#param $y
${THROWS_DOC}
A generated PHP Doc fragment containing exceptions thrown from function (method) body in the form * #throws ExceptionName. One exception per line/#throws tag. For example:
#throws DOMException
#throws HttpException
Code Templates for Overridden/Implemented Methods
The following templates can be found under Settings | File Templates | Code: PHP Implemented Method Body and PHP Overridden Method Body. There are few parameters, considering that in most cases we will need either a simple call to a parent method or just our own comment (some version of TODO perhaps):
${NAME}
Method name.
${PARAM_LIST}
A comma-separated list of parameters. For example, if the original method signature is foo(Bar $bar, $y), the variable will take the value “$bar, $x” which can be used in a call to the parent method as `${NAME}(${PARAM_LIST})`”
${RETURN}
Either “return” or an empty string.
A Dollar Sign Variable: ${DS}
Solves the problem of putting a dollar sign $ anywhere within the template. Actually, the dollar sign is used both in PHP and in Velocity template engine taking care of code generation behind the scenes. So whenever we need a dollar sign, just use ${DS} as its equivalent. For example, if we want $this->foo() to be generated, we need to put ${DS}this->foo(). This may not look perfect but guarantees that there will be no conflicts.

How to (accidentally) write into the global context (namespace) with coffeescript?

I accidentally stumbled over the following code that actually modified the global namespace:
I though this is not possible?
The following code writes three variables into the global namespace (try it):
this.my_global1=1
#my_global2=2
f= -> #my_global3=3
f()
If you now replace the above code with this in the cofeescript try page
alert("#{[my_global1,my_global2,my_global3]}")
you will see an alert with
1,2,3
This means the statements above modify the global context!
It took me many hours to figure out what was going wrong with my code, because I thought coffeescript protects me form accidental changes of the global environment!
CoffeeScript can't prevent you from doing this, but JavaScript can. Use strict mode:
do ->
"use strict"
this.$ = 3
In non-strict mode, this defaults to window if it isn't specified when the function is called. In strict mode, this becomes undefined, which will throw an error if you try to assign properties to it:
TypeError: Cannot set property '$' of undefined

iOS 6 - HTML encoded entities are not showing

I am trying to show following HTML encoded form of string in UI.
ひょ䥜 姌祦橯ね褎 れずしゅぎゅ穃 ぢょ簨ゝじ氯 苯䛣蟤 覵だ盨ꤎ拣 夯ぢゃ焯埥榥 楎べ楚 坩づ, 覌びょ は䦞びぢじょ 氧へヴェ栤儦 ぢゅ大廩 ぷ諥 礊䛨にょ 襦綦卣楌䨵 が椧りゅ樃驌 蟥ゞ睥盥䨣 騌捯 驩ぎょちゅ レ゜㩟カグォ階 猪仯壪黨ザ れずしゅぎゅ穃, お䤂 ぴゅにゅ䧣鏨以 コざちゃ㠤ジョ 苨䰯お
Using this library, I wrote following code
#import "NSString+HTML.h"
NSLog(#"%#",plainText);
NSLog(#"%#",[plainText stringByDecodingHTMLEntities]);
On executing code, I am getting following exception:
-[__NSCFString stringByDecodingHTMLEntities]: unrecognized selector sent to instance 0x7bd4200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString stringByDecodingHTMLEntities]: unrecognized selector sent to instance 0x7bd4200'
*** First throw call stack:
(0x1caa012 0x10e7e7e 0x1d354bd 0x1c99bbc 0x1c9994e 0xa72d 0x4a0e53f 0x4a20014 0x4a107d5 0x1c50af5 0x1c4ff44 0x1c4fe1b 0x1c047e3 0x1c04668 0x2bffc 0x237d 0x22a5 0x1)
libc++abi.dylib: terminate called throwing an exception
Note: I also tested my HTML encoded string on this link and found no errors in my string format.
#import "NSString+HTML.h"
NSLog(#"%#",plainText);
NSLog(#"%#",[plainText stringByDecodingHTMLEntities]);
You need add NSString+HTML.h, NSString+HTML.m, GTMNSString+HTML.h, GTMNSString+HTML.m file to your project
add -fno-objc-arc
Build Phases -> Compile Sources -> select GTMNSString+HTML.m -> double click right Compiler flags and add -fno-objc-arc
Things were perfectly fine. All I needed was just to add compile source flag -fno-objc-arc by double clicking NSString+HTML.m and GTMNSString+HTML.m under Compile Source tab.
Just imported NSString+HTML.m and no need to import other class
Drag and drop NSString+HTML.m and GTMNSString+HTML.m in Compile Source tab first before adding flag.