Explicit typing on property of instance - phpstorm

To help my editor better understand my code, I sometimes need to add a comment like this:
/* #var $container Container */
This work good, but sometimes I would need something like this:
/* #var $this->container Container */
Is there something like this?

A few things first:
1. PHPDoc comments start with /**.
For compatibility reasons PhpStorm understands PHPDoc tags in ordinary /* comments as well, but you better use the correct symbols for them.
2. The correct order of elements for inline #var tag can be seen here:
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#517-var
/** #var [type] [element_name] [<optional description>] */
e.g.
/** #var Container $container */
Just as with #1: PhpStorm understands such comments even if elements are swapped around (for compatibility with other (old) IDEs / old code).
Your actual question:
Inline #var allows typehinting only local / ordinary variables. You CANNOT use it for compound variables (cannot use $this->container or $someObject->someVar here).
This is wrong:
/* #var $this->container Container */
// even if it uses correct order/style
/** #var Container $this->container */
If anything: such typehints should be provided in the actual class, above the actual property declaration (where you omit the [element_name] part) https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#examples-15
class MyAwesomeClass
{
/** #var Container Optional description here */
protected $container;
...
}

Related

Assign color value to css variables

In my css i have several color variables like:
$bkg-color: #ffffff
white is what i want the default color to be but the $bkg-color could also be customized to a different color based on what the user creates and saves. So in my typescript i have:
backgroundColor?: string = '';
my get call response data is set to pageDetails and then i check
if(pageDetails.backgroundColor) {
this.backgroundColor = pageDetails.backgroundColor;
}
I cannot directly bind the background color class to the html because my page content is displayed via v-html='displayedHtml'
How can i set $bkg-color: this.backgroundColor (if exists || set default color of white).
i am using vue and typescript
i believe i can set the default like so: $bkg-color: #ffffff !default; but how i can make it assigned based on my data is where i am a bit lost.
How can i set $bkg-color: this.backgroundColor (if exists || set default color of white).
Unfortunately, Scss variables do not exist at runtime, they exist only at compile time and then the python-written compiler replaces them with their value in the css style sheet. If you're not supporting Internet Explorer You can use css variables to solve this problem which can be changed at run time dynamically.
If iv'e misunderstood the question let me know and i'll add an edit.
EDIT:
https://jsfiddle.net/njsrLywt/ Here's a demo showing changing css variables at run-time.
EDIT2: You will also no longer be able to use lighten/darken however you can use a javascript implementation of lighten/darken which exists at run-time. Also on a side note you should avoid using lighten/darken in favour of scale-color which allows for better color scaling because it scales the remaining % of 100
EDIT3: Here is a quick implemtation of scale-color implemented with javascript 100% fully immutable FP style use it in the following way
ColorUtils.scaleColor("#294e80", -35); //darken 35%
ColorUtils.scaleColor("#F6F6F6", 35); // lighten 35%
https://typescript-play.js.org/#code/MYGwhgzhAEDCD2J4CcCqAXAliGBvAUAJATphbDQTBggCmCSy0AvNABTCIoAStAHgC5K6ZJgB2AcwA00AA61kwWmPRCxAVwC2AIwUBKFgD5oBQoQD0AKmiZ0AchgALMMgAm0dPGgSvAM2TwmtCO-NAAtMaOECDQYMDA6shktCAAnpRemN5+AUEhfOHGyBLahdBitADu0NwAygAyZfnQluZEhJxiJNAASgDiAEIswfwAKvA9JRxcyLx8egDc7Zi+7P1DzJvl6iAgBsi06Ili0JyMc0tmnd0A2o4yEDIgALrDxdoATFEgbDfrAHTIGQA6S9Qb-bTPRbLVZseSKZToaDGAAMBlMV3gXSRFUq9UwEkc6AqUGGMQA1OwAIwolHhaB7FrseFKFTQczQGloy5mA5HZAnb7jOZse6UGS4-GE4m0KDQswAXxhzIUrKRAB5oGiTO0OljupKCUSSTBWBSGUy4arEezObT5bzDsdgtFhfxRQ8JVUpcbZRAHUrHfyTmcePxLgqlkr8OYOc0ItB3k1ok1+Phrkj8uNJqVWKL+EISKJJAZmMZTBnKI4UOhnGJXD1aBJQqxzAA9ADEAH5fmAwr4ADquKG9-tDkc3PuD4d6AAk5kwSyIldxc2G+UBtFk4CUbAg1eQtbA9cbzb4MjYmkLInEoOQ1+LoIkD9vMm0L5LRh1QedTEpf+8aBKQkIDoFKSltAjaFl31JEDggHYkVbTse0nMdh1wD4FT0Udp2eTDsNw8cCLnBd-n4WhgDYVd+HlPlf1lRD2i7b9eSEWQXAgWgAEkVDYeDEJuKlnhkKkADY9CkXVnzkTieL4gSQHQG4PhEzkJKksxCHfWTkC43j0H4xilJu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9Y2AFGsKVPQ-xPC1BvJINglkemgxGHwf4dBJC1hYFsOKLEABEKJXmgWaEIfIw8rDQAAKIADkAAi0BuKdSifUAFnVgWjA8qMbiZCgUtDaB7IAA

POO Propperty getter / setter vs Function (Theory)

So if a getter is finally a function, why not use just a function? I guess to enforce using that function to set/get a value any time needed, any other reason?
A getter is a function (as far as POO languages I know).
The idea is to delegate the ability to modify or to get an attribute to only one function by class, in case of there were special checks to perform before applying the getter or setter.
Properties are used just to improve readability or ease the API, without breaking encapsulation. For example, if you have a Rectangle class and you want to increase by 10 the width of an instance, without properties, you would do something like:
rect.setWidth(rect.getWidth() + 10)
While, using properties, you could just write:
rect.width += 10
Which is clearer. And because properties are functions, you can do all the checks that you need. Moreover, it doesn't even matter if the width is actually stored in the class; maybe the rectangle is stored as a pair of points (i.e. upper-left and lower-right), but the property would handle it. Additionally, you could write properties that access the same data in different ways: for example, you could have width, height, top, bottom, left and right properties, and all of them would affect the same internal data of the class, but would make it easier to use and more readable.

addChild(Button) in AS3

I am writing a class that extends mx.core.UIComponent. In the constructor of this class, I would like to add a Button (spark.components.Button). However, creating a new Button object (such as var b:Button = new Button();) and then doing this.addChild(b) doesn't work -- no button is appearing. Is there any reason why a Button object can't be added as a child?
Edit: Just to add, when I print out the children, it says that the Button object is a child, but the Button doesn't show up.
You need to add the button in the createChildren method not in the constructor.
protected var button:Button;
override protected function createChildren():void {
super.createChildren();
button = new Button();
addChild(button);
}
Depending on what you are trying to do you may also need to implement the measure and updateDisplayList methods.
Using UIComponent directly, you aren't getting the layout of the child handled for you; you would need to set the position and size of your child components manually (see docs).
Consider using a Group (or Canvas, if you're still on MX components) to handle child layout. You can still add rendering in updateDisplayList, just make sure you call super.updateDisplayList() as well.
You need to read up on the differences between ActionScript components and Flex components. When using a Flex component, you must use container.addElement( element ), not container.addChild( child ). As far as I am aware, the only classes with this functionality descend from Group (at least in the Spark framework, the one I am most familiar with). So extending UIComponent will not allow you to add a Button, although a UIComponent can be added to a Flex container (which I do not believe a normal Sprite can do)

HTML: How can i do this...!

I am developing an application where i have to display names of users in following structure :
In above structure, in the name field, it may exceed the right border of the outer <div> tag, i want to cut the name value just before it touches the right border and append the a string '...' in the end just like below
How can i make it work for UTF-8, unicode or Normal english letters in the name field?
P.S. I m using PHP for server side processing.
The easiest way is to set some CSS on that div...
<div style="white-space:nowrap; text-overflow:ellipsis;">Er. Christopher Allen (ChristAllenMoreTextMoreText)</div>
You have to set the white-space attribute as well, otherwise you won't ever get to this elipsis point. You should also probably set overflow:hidden.
Big caveat though! This does not work in all browsers. IE7 and beyond, Safari/Chrome, are all fine. I believe there are issues with it in Firefox though.
Edit: Here is a Firefox workaround. Not amazing though.
Since you can't know the exact width of the container on the client side (in the user's browser), you must use Javascript for this.
You can't just let it wrap?
Perfecting a server-side character limiter so it cuts off just before the line-end will be tough, if not impossible, unless using a monospace font. And you can't account for the user's browser settings, so the solution will always be flawed. If any character limiter is created, it will need to be client-side.
What about setting overflow: hidden on the name element?
I personally do mine on the back-end with PHP using the following custom function:
function trunc($string, $limit, $break=" ", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}
Sure, it's not terribly schnazzy, and it doesn't exactly auto adjust, but my project requirments demand that everything works 100% correctly in IE6 and this works without fail.
If you HAVE to do it on the front end, try 3 dots: http://tpgblog.com/threedots/
As Brad suggested: CSS is the way to go. Flexible presentation is better handled on the client side. However, most browsers do not implement this specific CSS3 feature (yet). In the meantime you could try one of these jQuery (javascript) plugins that makes the same functionality available to all browsers.
http://plugins.jquery.com/plugin-tags/ellipsis
http://plugins.jquery.com/project/shorten
http://plugins.jquery.com/project/ThreeDots
http://plugins.jquery.com/project/text-overflow
http://plugins.jquery.com/project/AutoEllipsis
Are you using a database? If so you can write a conditional statement to check if the name is over a certain length and if so append ...
You might want to define your width in Em and then count characters PHP side to approximate where you need to cut and append your ellipses.
In effect you need to work out the pixel length of the string and keep chopping chars until it falls below the threshold. I could explain that on the server side if you had .net at hand, but you dont, so maybe a this javascript might help on the client : Truncate a string nicely to fit within a given pixel width

Flex: How does a component know whether one of its styles got changed?

I inherited a custom component from TextField. The component needs to know when any of its styles got changed at runtime via setStyle. How would I do that? It's probably obvious but I couldn't find an event or appropriate method to override.
If you want the text field to play nicely with containers and other components in Flex you may want to wrap it in a UIComponent, or have the subclass implement the IUIComponent and IStyleClient or ISimpleStyleClient interfaces (which UIComponent implements). If you do the component will work with Flex' style system and every time a style changes a method calledstyleChanged` will be called:
public function styleChanged(styleProp:String):void
See http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#styleChanged()
styleChanged () method
public function styleChanged(styleProp:String):void
Detects changes to style properties. When any style property is set, Flex calls the styleChanged() method, passing to it the name of the style being set.
This is an advanced method that you might override when creating a subclass of UIComponent. When you create a custom component, you can override the styleChanged() method to check the style name passed to it, and handle the change accordingly. This lets you override the default behavior of an existing style, or add your own custom style properties.
If you handle the style property, your override of the styleChanged() method should call the invalidateDisplayList() method to cause Flex to execute the component's updateDisplayList() method at the next screen update.
Parameters styleProp:String — The name of the style property, or null if all styles for this component have changed.