How to achieve hard-code voilations with CodePro? - checkstyle

I have to check some voilations like this
if(emp.companyName="MyCompany")
ie. Here there should not "MyComapany" hard-code in expression or condition checking.
which rule of CodePro do this.

PMD has a rule for similar problems that you described:
AvoidDuplicateLiterals - Code containing duplicate String literals can usually be improved by declaring the String as a constant field.

Related

Junit using eq() argument matcher vs passing string directly

What is the use of eq() argument matcher if passing the string directly will do the same thing.
e.g. the behaviour of
when(method.foo("test")).thenReturn("bar");
is similar to
when(method.foo(ArgumentMatcher.eq("test"))).thenReturn("bar");
There are more ArgumentMatchers than eq(). Another popular one is any(), but there are many more ArgumentMatchers. They are generally used together to help identify the correct value for the test case. You may not want to check all args in all tests. For example, if there were more params in your code.
when(method.foo(eq("test"), any(Test.class), isNull()).thenReturn("bar");
I agree that eq() seems redundant, but the trick is if one argument uses a matcher all must, so if you want to use one any() you can no longer just put an unwrapped String argument.

Chef - expressions in `kitchen.yml` attributes?

In kitchen.yml, I would like to have an expression in the attributes: part. However it seems it is just a static file with literal values.
Is it somehow possible to have the values in attributes: evaluated?
The reason for that need is that I have some node.defaults in defaults.rb, and some of them are URLs at the same host, say, http:foo.org/service. And in the kitchen.yml I want to parametrize the host. So I would have:
...
attributes: { serviceX_baseURL: "http://bar.org/service" }
I want the override to happen with kitchen_*.yml override and not attributes/*.rb (that would be easier) because the override happens later in the process, after the main kitchen.yml file is already generated.
Any practical solutions for that are welcome.
You can use Erb formatting in the .kitchen.yml for some very simplistic templating, but you didn't really give a concrete example. Chances are you should not do this, generally parameterizing both the code and tests on the same input means the tests are brittle or not testing what you think they are.

What is "Ambiguous regexp literal" in rubocop?

The following code
expect(foo).to match /#{MyGem.config.environment_name}/
triggers a rubocop issue
Warning: Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the / if it should be a division.
Can someone explain what the issue is and how to resolve it?
Another way to fix this is to simply add parens as rubocop suggests. Change
expect(foo).to match /#{MyGem.config.environment_name}/
to
expect(foo).to match(/#{MyGem.config.environment_name}/)
It is complaining that at this point:
match /#{MyGem.config.environment_name}/
it is unclear whether you're dividing two numbers or passing a RegExp literal to a method call.
In this particulare case, since you're just checking for the presence of a substring within a string, it would be better to use the RSpec #include matcher, like so:
expect(foo).to include MyGem.config.environment_name

mySQL replace string + additional string with a static value

Unlike PHP, I don't believe mySQL has any preg_replace() feature, only matching via REGEXP. Here are the strings I have in the code:
http://ourcompany.com/theapplestore/...
http://ourcompany.com/anotherstore/...
http://ourcompany.com/yetanotherstore/...
As you can see, there is a constant in there, http://ourcompany.com/, but there is also a variable string namely theapplestore, anotherstore, etc. etc.
I want to replace the constant string, plus the variable string(s), and then the trailing slash (/) after the variable string(s), with a single shortcode value, namely {{store url=''}}
EDIT
If it helps, the store codes are always the same length, they are going to be
sch131785
sch185399
sch634019
etc.
i.e., they are all 9 characters long
How would I do this? Thanks.
I thought this might be useful: there is currently NO WAY to do this in mysql. Find using REGEXP, yes; replace, no. That said, there is another post with an extension library mentioned, sagi:
Is there a MySQL equivalent of PHP's preg_replace?
MariaDB-10.0.5 has REGEXP_REPLACE(), REGEXP_INSTR() and REGEXP_SUBSTR()
You can use following regex,
(ourcompany.com\/\w+\/)
Demo
Uses the concept of Group Capture

ActionScript * datatype - what is it?

In Flex I can specify a * as datatype.
What is it and how should I use it.
I've written a inArray() method that takes as argument something to search for and something to search in. The "something to search for" looks like a great candidate for the * datatype.
Should I not use it?
Thank you.
* indicates an untyped identifier. You'd use it when you can't guarantee that the type of an object will conform to any specific class or interface definition.
Whether * is appropriate in your example depends on what operations you use in your search. If you just rely on common equality operations, you should be fine, and in fact it would probably be the best choice.
The * is to AS3 as Object is to Java. See the following:
http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_11.html
The * actually explicitly says, that something has no type. In fact it's the default type, all untyped variables or functions fall back to.
It basically tells the compiler: "I don't want you to perform any typecheck on this, because this simply gets handled at runtime".
In fact * is no type. There is simply no value corresponding to it.
Please note, that only expressions of type * can return the value of undefined. Anything, that is of type Object (i.e. anything that is of a different type, than no type), can only hold null.
You will see that many Array methods use that type. That's specifically, because Array elements are of no type and can thus be undefined.
Therefore, I suppose you could have a slight performance difference based on whether your parameter is typed * or Object (or whether you use strict comparison or not, for that matter).
Personally, I would advise you use * in this case, mostly, because it is consistent with Array methods.
you have to use * when strict mode of compilation is enabled. It's just 'no type', and it will be checked at runtime.