Page MenuHomeSealhub

No OneTemporary

diff --git a/resources/php_compat_info.json b/resources/php_compat_info.json
index 876c0209..b752ec50 100644
--- a/resources/php_compat_info.json
+++ b/resources/php_compat_info.json
@@ -1 +1,284 @@
-{"@generated":true,"params":{"is_a":{"2":"5.3.9"},"nl2br":{"1":"5.3.0"},"getopt":{"1":"5.3.0"},"copy":{"2":"5.3.0"},"opendir":{"1":"5.3.0"},"clearstatcache":["5.3.0","5.3.0"],"scandir":{"2":"5.3.0"},"json_decode":{"2":"5.3.0","3":"5.4.0"}},"functions":{"ldap_control_paged_result":"5.4.0","ldap_control_paged_result_response":"5.4.0","date_add":"5.3.0","date_create_from_format":"5.3.0","date_diff":"5.3.0","date_get_last_errors":"5.3.0","date_interval_create_from_date_string":"5.3.0","date_interval_format":"5.3.0","date_parse_from_format":"5.3.0","date_sub":"5.3.0","date_timestamp_get":"5.3.0","date_timestamp_set":"5.3.0","timezone_location_get":"5.3.0","timezone_version_get":"5.3.0","msg_queue_exists":"5.3.0","openssl_cipher_iv_length":"5.3.3","openssl_decrypt":"5.3.0","openssl_dh_compute_key":"5.3.0","openssl_digest":"5.3.0","openssl_encrypt":"5.3.0","openssl_get_cipher_methods":"5.3.0","openssl_get_md_methods":"5.3.0","openssl_random_pseudo_bytes":"5.3.0","socket_import_stream":"5.4.0","class_alias":"5.3.0","gc_collect_cycles":"5.3.0","gc_enabled":"5.3.0","gc_enable":"5.3.0","gc_disable":"5.3.0","get_called_class":"5.3.0","get_declared_traits":"5.4.0","trait_exists":"5.4.0","array_replace":"5.3.0","array_replace_recursive":"5.3.0","forward_static_call":"5.3.0","forward_static_call_array":"5.3.0","gethostname":"5.3.0","getimagesizefromstring":"5.4.0","header_register_callback":"5.4.0","header_remove":"5.3.0","hex2bin":"5.4.0","http_response_code":"5.4.0","lcfirst":"5.3.0","parse_ini_string":"5.3.0","php_ini_loaded_file":"5.2.4","quoted_printable_encode":"5.3.0","realpath_cache_get":"5.3.2","realpath_cache_size":"5.3.2","str_getcsv":"5.3.0","stream_context_get_params":"5.3.0","stream_context_set_default":"5.3.0","stream_set_read_buffer":"5.3.3","stream_is_local":"5.2.4","stream_resolve_include_path":"5.3.2","stream_set_chunk_size":"5.4.0","stream_supports_lock":"5.3.0","pg_escape_literal":"5.4.4","pg_escape_identifier":"5.4.4","gmp_testbit":"5.3.0","preg_filter":"5.3.0","hash_copy":"5.3.0","shm_has_var":"5.3.0","gzdecode":"5.4.0","zlib_decode":"5.4.0","zlib_encode":"5.4.0","pcntl_signal_dispatch":"5.3.0","pcntl_sigprocmask":"5.3.0","pcntl_sigtimedwait":"5.3.0","pcntl_sigwaitinfo":"5.3.0","pcntl_get_last_error":"5.3.4","pcntl_errno":"5.3.4","pcntl_strerror":"5.3.4","enchant_broker_describe":"5.3.0","enchant_broker_dict_exists":"5.3.0","enchant_broker_free_dict":"5.3.0","enchant_broker_free":"5.3.0","enchant_broker_get_error":"5.3.0","enchant_broker_init":"5.3.0","enchant_broker_request_dict":"5.3.0","enchant_broker_request_pwl_dict":"5.3.0","enchant_broker_set_ordering":"5.3.0","enchant_dict_add_to_personal":"5.3.0","enchant_dict_add_to_session":"5.3.0","enchant_dict_check":"5.3.0","enchant_dict_describe":"5.3.0","enchant_dict_get_error":"5.3.0","enchant_dict_is_in_session":"5.3.0","enchant_dict_store_replacement":"5.3.0","enchant_dict_suggest":"5.3.0","enchant_dict_quick_check":"5.3.0","enchant_broker_list_dicts":"5.3.0","enchant_broker_set_dict_path":"5.3.1","enchant_broker_get_dict_path":"5.3.1","imap_fetchmime":"5.3.6","imap_gc":"5.3.0","imap_utf8_to_mutf7":"5.3.0","imap_mutf7_to_utf8":"5.3.0","class_uses":"5.4.0","libxml_disable_entity_loader":"5.2.11","libxml_set_external_entity_loader":"5.4.0","json_last_error":"5.3.0","mb_encoding_aliases":"5.3.0","mb_ereg_replace_callback":"5.4.1","collator_asort":"5.2.4","collator_compare":"5.2.4","collator_create":"5.2.4","collator_get_attribute":"5.2.4","collator_get_error_code":"5.2.4","collator_get_error_message":"5.2.4","collator_get_locale":"5.2.4","collator_get_sort_key":"5.2.4","collator_get_strength":"5.2.4","collator_set_attribute":"5.2.4","collator_set_strength":"5.2.4","collator_sort":"5.2.4","collator_sort_with_sort_keys":"5.2.4","datefmt_create":"5.2.4","datefmt_format":"5.2.4","datefmt_get_calendar":"5.2.4","datefmt_get_datetype":"5.2.4","datefmt_get_error_code":"5.2.4","datefmt_get_error_message":"5.2.4","datefmt_get_locale":"5.2.4","datefmt_get_pattern":"5.2.4","datefmt_get_timetype":"5.2.4","datefmt_get_timezone_id":"5.2.4","datefmt_is_lenient":"5.2.4","datefmt_localtime":"5.2.4","datefmt_parse":"5.2.4","datefmt_set_calendar":"5.2.4","datefmt_set_lenient":"5.2.4","datefmt_set_pattern":"5.2.4","datefmt_set_timezone_id":"5.2.4","grapheme_extract":"5.2.4","grapheme_stripos":"5.2.4","grapheme_stristr":"5.2.4","grapheme_strlen":"5.2.4","grapheme_strpos":"5.2.4","grapheme_strripos":"5.2.4","grapheme_strrpos":"5.2.4","grapheme_strstr":"5.2.4","grapheme_substr":"5.2.4","intl_error_name":"5.2.4","intl_get_error_code":"5.2.4","intl_get_error_message":"5.2.4","intl_is_failure":"5.2.4","locale_accept_from_http":"5.2.4","locale_canonicalize":"5.2.4","locale_compose":"5.2.4","locale_filter_matches":"5.2.4","locale_get_all_variants":"5.2.4","locale_get_default":"5.2.4","locale_get_display_language":"5.2.4","locale_get_display_name":"5.2.4","locale_get_display_region":"5.2.4","locale_get_display_script":"5.2.4","locale_get_display_variant":"5.2.4","locale_get_keywords":"5.2.4","locale_get_primary_language":"5.2.4","locale_get_region":"5.2.4","locale_get_script":"5.2.4","locale_lookup":"5.2.4","locale_parse":"5.2.4","locale_set_default":"5.2.4","msgfmt_create":"5.2.4","msgfmt_format":"5.2.4","msgfmt_format_message":"5.2.4","msgfmt_get_error_code":"5.2.4","msgfmt_get_error_message":"5.2.4","msgfmt_get_locale":"5.2.4","msgfmt_get_pattern":"5.2.4","msgfmt_parse":"5.2.4","msgfmt_parse_message":"5.2.4","msgfmt_set_pattern":"5.2.4","normalizer_is_normalized":"5.2.4","normalizer_normalize":"5.2.4","numfmt_create":"5.2.4","numfmt_format":"5.2.4","numfmt_format_currency":"5.2.4","numfmt_get_attribute":"5.2.4","numfmt_get_error_code":"5.2.4","numfmt_get_error_message":"5.2.4","numfmt_get_locale":"5.2.4","numfmt_get_pattern":"5.2.4","numfmt_get_symbol":"5.2.4","numfmt_get_text_attribute":"5.2.4","numfmt_parse":"5.2.4","numfmt_parse_currency":"5.2.4","numfmt_set_attribute":"5.2.4","numfmt_set_pattern":"5.2.4","numfmt_set_symbol":"5.2.4","numfmt_set_text_attribute":"5.2.4","resourcebundle_count":"5.2.4","resourcebundle_create":"5.2.4","resourcebundle_get":"5.2.4","resourcebundle_get_error_code":"5.2.4","resourcebundle_get_error_message":"5.2.4","resourcebundle_locales":"5.2.4","transliterator_create":"5.4.0","transliterator_create_from_rules":"5.4.0","transliterator_create_inverse":"5.4.0","transliterator_get_error_code":"5.4.0","transliterator_get_error_message":"5.4.0","transliterator_list_ids":"5.4.0","transliterator_transliterate":"5.4.0","idn_to_ascii":"5.2.4","idn_to_utf8":"5.2.4","mysqli_error_list":"5.4.0","mysqli_refresh":"5.3.0","mysqli_stmt_error_list":"5.4.0","session_register_shutdown":"5.4.0","session_status":"5.4.0"},"classes":{"dateinterval":"5.3.0","dateperiod":"5.3.0","snmp":"5.4.0","closure":"5.3.0","reflectionzendextension":"5.4.0","sqlite3":"5.3.0","sqlite3stmt":"5.3.0","sqlite3result":"5.3.0","callbackfilteriterator":"5.4.0","recursivecallbackfilteriterator":"5.4.0","recursivetreeiterator":"5.3.0","filesystemiterator":"5.3.0","globiterator":"5.3.0","spldoublylinkedlist":"5.3.0","splqueue":"5.3.0","splstack":"5.3.0","splheap":"5.3.0","splminheap":"5.3.0","splmaxheap":"5.3.0","splpriorityqueue":"5.3.0","splfixedarray":"5.3.0","multipleiterator":"5.3.0","collator":"5.2.4","numberformatter":"5.2.4","locale":"5.2.4","normalizer":"5.2.4","messageformatter":"5.2.4","intldateformatter":"5.2.4","resourcebundle":"5.2.4","transliterator":"5.4.0","spoofchecker":"5.4.0","sessionhandler":"5.4.0"},"interfaces":{"jsonserializable":"5.4.0","sessionhandlerinterface":"5.4.0"},"functions_windows":{"apache_child_terminate":"","chroot":"","getrusage":"","imagecreatefromxpm":"","lchgrp":"","lchown":"","nl_langinfo":"","strptime":"","sys_getloadavg":"","checkdnsrr":"5.3.0","dns_get_record":"5.3.0","fnmatch":"5.3.0","getmxrr":"5.3.0","getopt":"5.3.0","imagecolorclosesthwb":"5.3.0","inet_ntop":"5.3.0","inet_pton":"5.3.0","link":"5.3.0","linkinfo":"5.3.0","readlink":"5.3.0","socket_create_pair":"5.3.0","stream_socket_pair":"5.3.0","symlink":"5.3.0","time_nanosleep":"5.3.0","time_sleep_until":"5.3.0"}}
\ No newline at end of file
+{
+ "@generated" : true,
+ "params" : {
+ "is_a" : {
+ "2" : "5.3.9"
+ },
+ "json_decode" : {
+ "2" : "5.3.0",
+ "3" : "5.4.0"
+ },
+ "assert" : {
+ "1" : "5.4.8"
+ },
+ "clearstatcache" : [
+ "5.3.0",
+ "5.3.0"
+ ],
+ "copy" : {
+ "2" : "5.3.0"
+ },
+ "nl2br" : {
+ "1" : "5.3.0"
+ },
+ "opendir" : {
+ "1" : "5.3.0"
+ },
+ "getopt" : {
+ "1" : "5.3.0"
+ }
+ },
+ "functions" : {
+ "apcu_add" : "5.3.0",
+ "apcu_bin_dump" : "5.3.0",
+ "apcu_bin_dumpfile" : "5.3.0",
+ "apcu_bin_load" : "5.3.0",
+ "apcu_bin_loadfile" : "5.3.0",
+ "apcu_cache_info" : "5.3.0",
+ "apcu_cas" : "5.3.0",
+ "apcu_clear_cache" : "5.3.0",
+ "apcu_dec" : "5.3.0",
+ "apcu_delete" : "5.3.0",
+ "apcu_exists" : "5.3.0",
+ "apcu_fetch" : "5.3.0",
+ "apcu_inc" : "5.3.0",
+ "apcu_sma_info" : "5.3.0",
+ "apcu_store" : "5.3.0",
+ "class_alias" : "5.3.0",
+ "gc_collect_cycles" : "5.3.0",
+ "gc_disable" : "5.3.0",
+ "gc_enable" : "5.3.0",
+ "gc_enabled" : "5.3.0",
+ "get_called_class" : "5.3.0",
+ "get_declared_traits" : "5.4.0",
+ "trait_exists" : "5.4.0",
+ "date_add" : "5.3.0",
+ "date_create_from_format" : "5.3.0",
+ "date_diff" : "5.3.0",
+ "date_get_last_errors" : "5.3.0",
+ "date_interval_create_from_date_string" : "5.3.0",
+ "date_interval_format" : "5.3.0",
+ "date_parse_from_format" : "5.3.0",
+ "date_sub" : "5.3.0",
+ "date_timestamp_get" : "5.3.0",
+ "date_timestamp_set" : "5.3.0",
+ "timezone_location_get" : "5.3.0",
+ "timezone_version_get" : "5.3.0",
+ "mime_content_type" : "5.3.0",
+ "imagecreatefromwebp" : "5.4.0",
+ "imagewebp" : "5.4.0",
+ "gmp_testbit" : "5.3.0",
+ "hash_copy" : "5.3.0",
+ "imap_fetchmime" : "5.3.6",
+ "imap_gc" : "5.3.0",
+ "imap_mutf7_to_utf8" : "5.3.0",
+ "imap_utf8_to_mutf7" : "5.3.0",
+ "datefmt_create" : "5.2.4",
+ "datefmt_format" : "5.2.4",
+ "datefmt_get_calendar" : "5.2.4",
+ "datefmt_get_datetype" : "5.2.4",
+ "datefmt_get_error_code" : "5.2.4",
+ "datefmt_get_error_message" : "5.2.4",
+ "datefmt_get_locale" : "5.2.4",
+ "datefmt_get_pattern" : "5.2.4",
+ "datefmt_get_timetype" : "5.2.4",
+ "datefmt_get_timezone_id" : "5.2.4",
+ "datefmt_is_lenient" : "5.2.4",
+ "datefmt_localtime" : "5.2.4",
+ "datefmt_parse" : "5.2.4",
+ "datefmt_set_calendar" : "5.2.4",
+ "datefmt_set_lenient" : "5.2.4",
+ "datefmt_set_pattern" : "5.2.4",
+ "datefmt_set_timezone_id" : "5.2.4",
+ "grapheme_extract" : "5.2.4",
+ "grapheme_stripos" : "5.2.4",
+ "grapheme_stristr" : "5.2.4",
+ "grapheme_strlen" : "5.2.4",
+ "grapheme_strpos" : "5.2.4",
+ "grapheme_strripos" : "5.2.4",
+ "grapheme_strrpos" : "5.2.4",
+ "grapheme_strstr" : "5.2.4",
+ "grapheme_substr" : "5.2.4",
+ "locale_accept_from_http" : "5.2.4",
+ "idn_to_ascii" : "5.2.4",
+ "idn_to_utf8" : "5.2.4",
+ "collator_get_sort_key" : "5.2.4",
+ "resourcebundle_count" : "5.2.4",
+ "resourcebundle_create" : "5.2.4",
+ "resourcebundle_get" : "5.2.4",
+ "resourcebundle_get_error_code" : "5.2.4",
+ "resourcebundle_get_error_message" : "5.2.4",
+ "resourcebundle_locales" : "5.2.4",
+ "transliterator_create" : "5.4.0",
+ "transliterator_create_from_rules" : "5.4.0",
+ "transliterator_create_inverse" : "5.4.0",
+ "transliterator_get_error_code" : "5.4.0",
+ "transliterator_get_error_message" : "5.4.0",
+ "transliterator_list_ids" : "5.4.0",
+ "transliterator_transliterate" : "5.4.0",
+ "json_last_error" : "5.3.0",
+ "ldap_control_paged_result" : "5.4.0",
+ "ldap_control_paged_result_response" : "5.4.0",
+ "event_add" : "5.3.0",
+ "event_base_free" : "5.3.0",
+ "event_base_loop" : "5.3.0",
+ "event_base_loopbreak" : "5.3.0",
+ "event_base_loopexit" : "5.3.0",
+ "event_base_new" : "5.3.0",
+ "event_base_priority_init" : "5.3.0",
+ "event_base_set" : "5.3.0",
+ "event_buffer_base_set" : "5.3.0",
+ "event_buffer_disable" : "5.3.0",
+ "event_buffer_enable" : "5.3.0",
+ "event_buffer_fd_set" : "5.3.0",
+ "event_buffer_free" : "5.3.0",
+ "event_buffer_new" : "5.3.0",
+ "event_buffer_priority_set" : "5.3.0",
+ "event_buffer_read" : "5.3.0",
+ "event_buffer_timeout_set" : "5.3.0",
+ "event_buffer_watermark_set" : "5.3.0",
+ "event_buffer_write" : "5.3.0",
+ "event_del" : "5.3.0",
+ "event_free" : "5.3.0",
+ "event_new" : "5.3.0",
+ "event_set" : "5.3.0",
+ "event_timer_add" : "5.3.0",
+ "event_timer_del" : "5.3.0",
+ "event_timer_new" : "5.3.0",
+ "event_timer_pending" : "5.3.0",
+ "event_timer_set" : "5.3.0",
+ "event_buffer_set_callback" : "5.3.0",
+ "event_priority_set" : "5.3.0",
+ "libxml_disable_entity_loader" : "5.2.11",
+ "libxml_set_external_entity_loader" : "5.4.0",
+ "mb_encoding_aliases" : "5.3.0",
+ "mb_ereg_replace_callback" : "5.4.1",
+ "mysqli_error_list" : "5.4.0",
+ "mysqli_refresh" : "5.3.0",
+ "mysqli_stmt_error_list" : "5.4.0",
+ "openssl_cipher_iv_length" : "5.3.3",
+ "openssl_decrypt" : "5.3.0",
+ "openssl_dh_compute_key" : "5.3.0",
+ "openssl_digest" : "5.3.0",
+ "openssl_encrypt" : "5.3.0",
+ "openssl_get_cipher_methods" : "5.3.0",
+ "openssl_get_md_methods" : "5.3.0",
+ "openssl_random_pseudo_bytes" : "5.3.0",
+ "pcntl_errno" : "5.3.4",
+ "pcntl_get_last_error" : "5.3.4",
+ "pcntl_signal_dispatch" : "5.3.0",
+ "pcntl_sigprocmask" : "5.3.0",
+ "pcntl_sigtimedwait" : "5.3.0",
+ "pcntl_sigwaitinfo" : "5.3.0",
+ "pcntl_strerror" : "5.3.4",
+ "preg_filter" : "5.3.0",
+ "pg_escape_identifier" : "5.4.4",
+ "pg_escape_literal" : "5.4.4",
+ "session_register_shutdown" : "5.4.0",
+ "session_status" : "5.4.0",
+ "socket_import_stream" : "5.4.0",
+ "class_uses" : "5.4.0",
+ "php_ini_loaded_file" : "5.2.4",
+ "stream_is_local" : "5.2.4",
+ "array_replace" : "5.3.0",
+ "array_replace_recursive" : "5.3.0",
+ "forward_static_call" : "5.3.0",
+ "forward_static_call_array" : "5.3.0",
+ "gethostname" : "5.3.0",
+ "header_remove" : "5.3.0",
+ "lcfirst" : "5.3.0",
+ "parse_ini_string" : "5.3.0",
+ "quoted_printable_encode" : "5.3.0",
+ "str_getcsv" : "5.3.0",
+ "stream_context_get_params" : "5.3.0",
+ "stream_context_set_default" : "5.3.0",
+ "stream_supports_lock" : "5.3.0",
+ "realpath_cache_get" : "5.3.2",
+ "realpath_cache_size" : "5.3.2",
+ "stream_resolve_include_path" : "5.3.2",
+ "stream_set_read_buffer" : "5.3.3",
+ "getimagesizefromstring" : "5.4.0",
+ "header_register_callback" : "5.4.0",
+ "hex2bin" : "5.4.0",
+ "http_response_code" : "5.4.0",
+ "stream_set_chunk_size" : "5.4.0",
+ "msg_queue_exists" : "5.3.0",
+ "shm_has_var" : "5.3.0",
+ "gzdecode" : "5.4.0",
+ "zlib_decode" : "5.4.0",
+ "zlib_encode" : "5.4.0"
+ },
+ "classes" : {
+ "closure" : "5.3.0",
+ "dateinterval" : "5.3.0",
+ "dateperiod" : "5.3.0",
+ "gender\\gender" : "5.3.0",
+ "intldateformatter" : "5.2.4",
+ "resourcebundle" : "5.2.4",
+ "spoofchecker" : "5.4.0",
+ "transliterator" : "5.4.0",
+ "mongoresultexception" : "5.2.6",
+ "mongoclient" : "5.2.6",
+ "cond" : "5.3.0",
+ "mutex" : "5.3.0",
+ "stackable" : "5.3.0",
+ "thread" : "5.3.0",
+ "worker" : "5.3.0",
+ "reflectionzendextension" : "5.4.0",
+ "sessionhandler" : "5.4.0",
+ "snmp" : "5.4.0",
+ "snmpexception" : "5.4.0",
+ "filesystemiterator" : "5.3.0",
+ "globiterator" : "5.3.0",
+ "multipleiterator" : "5.3.0",
+ "recursivetreeiterator" : "5.3.0",
+ "spldoublylinkedlist" : "5.3.0",
+ "splfixedarray" : "5.3.0",
+ "splheap" : "5.3.0",
+ "splmaxheap" : "5.3.0",
+ "splminheap" : "5.3.0",
+ "splpriorityqueue" : "5.3.0",
+ "splqueue" : "5.3.0",
+ "splstack" : "5.3.0",
+ "callbackfilteriterator" : "5.4.0",
+ "recursivecallbackfilteriterator" : "5.4.0",
+ "sqlite3" : "5.3.0",
+ "sqlite3result" : "5.3.0",
+ "sqlite3stmt" : "5.3.0",
+ "varnishadmin" : "5.3.0",
+ "varnishstat" : "5.3.0",
+ "varnishlog" : "5.3.0",
+ "varnishexception" : "5.3.0"
+ },
+ "interfaces" : {
+ "jsonserializable" : "5.4.0",
+ "sessionhandlerinterface" : "5.4.0"
+ },
+ "functions_windows" : {
+ "apache_child_terminate" : "",
+ "chroot" : "",
+ "getrusage" : "",
+ "imagecreatefromxpm" : "",
+ "lchgrp" : "",
+ "lchown" : "",
+ "nl_langinfo" : "",
+ "strptime" : "",
+ "sys_getloadavg" : "",
+ "checkdnsrr" : "5.3.0",
+ "dns_get_record" : "5.3.0",
+ "fnmatch" : "5.3.0",
+ "getmxrr" : "5.3.0",
+ "getopt" : "5.3.0",
+ "imagecolorclosesthwb" : "5.3.0",
+ "inet_ntop" : "5.3.0",
+ "inet_pton" : "5.3.0",
+ "link" : "5.3.0",
+ "linkinfo" : "5.3.0",
+ "readlink" : "5.3.0",
+ "socket_create_pair" : "5.3.0",
+ "stream_socket_pair" : "5.3.0",
+ "symlink" : "5.3.0",
+ "time_nanosleep" : "5.3.0",
+ "time_sleep_until" : "5.3.0"
+ }
+}
diff --git a/scripts/update_compat_info.php b/scripts/update_compat_info.php
index c85b3459..f0ad404a 100755
--- a/scripts/update_compat_info.php
+++ b/scripts/update_compat_info.php
@@ -1,76 +1,76 @@
#!/usr/bin/env php
<?php
require_once dirname(__FILE__).'/__init_script__.php';
$target = 'resources/php_compat_info.json';
echo "Purpose: Updates {$target} used by ArcanistXHPASTLinter.\n";
$ok = include 'PHP/CompatInfo/Autoload.php';
if (!$ok) {
echo "You need PHP_CompatInfo available in 'include_path'.\n";
echo "http://php5.laurent-laville.org/compatinfo/\n";
exit(1);
}
$required = '5.2.3';
$reference = id(new PHP_CompatInfo_Reference_ALL())->getAll();
$output = array();
$output['@'.'generated'] = true;
$output['params'] = array();
foreach (array('functions', 'classes', 'interfaces') as $type) {
$output[$type] = array();
foreach ($reference[$type] as $name => $versions) {
$name = strtolower($name);
$versions = reset($versions);
list($min, $max) = $versions;
if (version_compare($min, $required) > 0) {
$output[$type][$name] = $min;
}
- if ($type == 'functions' && isset($versions[2])) {
- $params = explode(', ', $versions[2]);
+ if ($type == 'functions' && isset($versions[4])) {
+ $params = explode(', ', $versions[4]);
foreach ($params as $i => $version) {
if (version_compare($version, $required) > 0) {
$output['params'][$name][$i] = $version;
}
}
}
}
}
// Grepped from PHP Manual.
$output['functions_windows'] = array(
'apache_child_terminate' => '',
'chroot' => '',
'getrusage' => '',
'imagecreatefromxpm' => '',
'lchgrp' => '',
'lchown' => '',
'nl_langinfo' => '',
'strptime' => '',
'sys_getloadavg' => '',
'checkdnsrr' => '5.3.0',
'dns_get_record' => '5.3.0',
'fnmatch' => '5.3.0',
'getmxrr' => '5.3.0',
'getopt' => '5.3.0',
'imagecolorclosesthwb' => '5.3.0',
'inet_ntop' => '5.3.0',
'inet_pton' => '5.3.0',
'link' => '5.3.0',
'linkinfo' => '5.3.0',
'readlink' => '5.3.0',
'socket_create_pair' => '5.3.0',
'stream_socket_pair' => '5.3.0',
'symlink' => '5.3.0',
'time_nanosleep' => '5.3.0',
'time_sleep_until' => '5.3.0',
);
file_put_contents(
phutil_get_library_root('arcanist').'/../'.$target,
- json_encode($output));
+ id(new PhutilJSON())->encodeFormatted($output));
echo "Done.\n";
diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php
index ea900691..6726d0ab 100644
--- a/src/lint/linter/ArcanistXHPASTLinter.php
+++ b/src/lint/linter/ArcanistXHPASTLinter.php
@@ -1,2324 +1,2324 @@
<?php
/**
* Uses XHPAST to apply lint rules to PHP.
*
* @group linter
*/
final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
private $futures = array();
private $trees = array();
const LINT_PHP_SYNTAX_ERROR = 1;
const LINT_UNABLE_TO_PARSE = 2;
const LINT_VARIABLE_VARIABLE = 3;
const LINT_EXTRACT_USE = 4;
const LINT_UNDECLARED_VARIABLE = 5;
const LINT_PHP_SHORT_TAG = 6;
const LINT_PHP_ECHO_TAG = 7;
const LINT_PHP_CLOSE_TAG = 8;
const LINT_NAMING_CONVENTIONS = 9;
const LINT_IMPLICIT_CONSTRUCTOR = 10;
const LINT_DYNAMIC_DEFINE = 12;
const LINT_STATIC_THIS = 13;
const LINT_PREG_QUOTE_MISUSE = 14;
const LINT_PHP_OPEN_TAG = 15;
const LINT_TODO_COMMENT = 16;
const LINT_EXIT_EXPRESSION = 17;
const LINT_COMMENT_STYLE = 18;
const LINT_CLASS_FILENAME_MISMATCH = 19;
const LINT_TAUTOLOGICAL_EXPRESSION = 20;
const LINT_PLUS_OPERATOR_ON_STRINGS = 21;
const LINT_DUPLICATE_KEYS_IN_ARRAY = 22;
const LINT_REUSED_ITERATORS = 23;
const LINT_BRACE_FORMATTING = 24;
const LINT_PARENTHESES_SPACING = 25;
const LINT_CONTROL_STATEMENT_SPACING = 26;
const LINT_BINARY_EXPRESSION_SPACING = 27;
const LINT_ARRAY_INDEX_SPACING = 28;
const LINT_RAGGED_CLASSTREE_EDGE = 29;
const LINT_IMPLICIT_FALLTHROUGH = 30;
const LINT_PHP_53_FEATURES = 31;
const LINT_REUSED_AS_ITERATOR = 32;
const LINT_COMMENT_SPACING = 34;
const LINT_PHP_54_FEATURES = 35;
const LINT_SLOWNESS = 36;
const LINT_CLOSING_CALL_PAREN = 37;
const LINT_CLOSING_DECL_PAREN = 38;
const LINT_REUSED_ITERATOR_REFERENCE = 39;
const LINT_KEYWORD_CASING = 40;
public function getLintNameMap() {
return array(
self::LINT_PHP_SYNTAX_ERROR => 'PHP Syntax Error!',
self::LINT_UNABLE_TO_PARSE => 'Unable to Parse',
self::LINT_VARIABLE_VARIABLE => 'Use of Variable Variable',
self::LINT_EXTRACT_USE => 'Use of extract()',
self::LINT_UNDECLARED_VARIABLE => 'Use of Undeclared Variable',
self::LINT_PHP_SHORT_TAG => 'Use of Short Tag "<?"',
self::LINT_PHP_ECHO_TAG => 'Use of Echo Tag "<?="',
self::LINT_PHP_CLOSE_TAG => 'Use of Close Tag "?>"',
self::LINT_NAMING_CONVENTIONS => 'Naming Conventions',
self::LINT_IMPLICIT_CONSTRUCTOR => 'Implicit Constructor',
self::LINT_DYNAMIC_DEFINE => 'Dynamic define()',
self::LINT_STATIC_THIS => 'Use of $this in Static Context',
self::LINT_PREG_QUOTE_MISUSE => 'Misuse of preg_quote()',
self::LINT_PHP_OPEN_TAG => 'Expected Open Tag',
self::LINT_TODO_COMMENT => 'TODO Comment',
self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression',
self::LINT_COMMENT_STYLE => 'Comment Style',
self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch',
self::LINT_TAUTOLOGICAL_EXPRESSION => 'Tautological Expression',
self::LINT_PLUS_OPERATOR_ON_STRINGS => 'Not String Concatenation',
self::LINT_DUPLICATE_KEYS_IN_ARRAY => 'Duplicate Keys in Array',
self::LINT_REUSED_ITERATORS => 'Reuse of Iterator Variable',
self::LINT_BRACE_FORMATTING => 'Brace placement',
self::LINT_PARENTHESES_SPACING => 'Spaces Inside Parentheses',
self::LINT_CONTROL_STATEMENT_SPACING => 'Space After Control Statement',
self::LINT_BINARY_EXPRESSION_SPACING => 'Space Around Binary Operator',
self::LINT_ARRAY_INDEX_SPACING => 'Spacing Before Array Index',
self::LINT_RAGGED_CLASSTREE_EDGE => 'Class Not abstract Or final',
self::LINT_IMPLICIT_FALLTHROUGH => 'Implicit Fallthrough',
self::LINT_PHP_53_FEATURES => 'Use Of PHP 5.3 Features',
self::LINT_PHP_54_FEATURES => 'Use Of PHP 5.4 Features',
self::LINT_REUSED_AS_ITERATOR => 'Variable Reused As Iterator',
self::LINT_COMMENT_SPACING => 'Comment Spaces',
self::LINT_SLOWNESS => 'Slow Construct',
self::LINT_CLOSING_CALL_PAREN => 'Call Formatting',
self::LINT_CLOSING_DECL_PAREN => 'Declaration Formatting',
self::LINT_REUSED_ITERATOR_REFERENCE => 'Reuse of Iterator References',
self::LINT_KEYWORD_CASING => 'Keyword Conventions',
);
}
public function getLinterName() {
return 'XHP';
}
public function getLintSeverityMap() {
$disabled = ArcanistLintSeverity::SEVERITY_DISABLED;
$advice = ArcanistLintSeverity::SEVERITY_ADVICE;
$warning = ArcanistLintSeverity::SEVERITY_WARNING;
return array(
self::LINT_TODO_COMMENT => $disabled,
self::LINT_UNABLE_TO_PARSE => $warning,
self::LINT_NAMING_CONVENTIONS => $warning,
self::LINT_PREG_QUOTE_MISUSE => $advice,
self::LINT_BRACE_FORMATTING => $warning,
self::LINT_PARENTHESES_SPACING => $warning,
self::LINT_CONTROL_STATEMENT_SPACING => $warning,
self::LINT_BINARY_EXPRESSION_SPACING => $warning,
self::LINT_ARRAY_INDEX_SPACING => $warning,
self::LINT_IMPLICIT_FALLTHROUGH => $warning,
self::LINT_SLOWNESS => $warning,
self::LINT_COMMENT_SPACING => $advice,
self::LINT_CLOSING_CALL_PAREN => $warning,
self::LINT_CLOSING_DECL_PAREN => $warning,
self::LINT_REUSED_ITERATOR_REFERENCE => $warning,
self::LINT_KEYWORD_CASING => $warning,
// This is disabled by default because it implies a very strict policy
// which isn't necessary in the general case.
self::LINT_RAGGED_CLASSTREE_EDGE => $disabled,
// This is disabled by default because projects don't necessarily target
// a specific minimum version.
self::LINT_PHP_53_FEATURES => $disabled,
self::LINT_PHP_54_FEATURES => $disabled,
);
}
protected function buildFutures(array $paths) {
foreach ($paths as $path) {
if (!isset($this->futures[$path])) {
$this->futures[$path] = xhpast_get_parser_future($this->getData($path));
}
}
return array_select_keys($this->futures, $paths);
}
public function getXHPASTTreeForPath($path) {
if (!array_key_exists($path, $this->trees)) {
$this->trees[$path] = null;
try {
$this->trees[$path] = XHPASTTree::newFromDataAndResolvedExecFuture(
$this->getData($path),
$this->futures[$path]->resolve());
$root = $this->trees[$path]->getRootNode();
$root->buildSelectCache();
$root->buildTokenCache();
} catch (XHPASTSyntaxErrorException $ex) {
$this->raiseLintAtLine(
$ex->getErrorLine(),
1,
self::LINT_PHP_SYNTAX_ERROR,
'This file contains a syntax error: '.$ex->getMessage());
} catch (Exception $ex) {
$this->raiseLintAtPath(self::LINT_UNABLE_TO_PARSE, $ex->getMessage());
}
}
return $this->trees[$path];
}
public function getCacheVersion() {
$version = '4';
$path = xhpast_get_binary_path();
if (Filesystem::pathExists($path)) {
$version .= '-'.md5_file($path);
}
return $version;
}
protected function resolveFuture($path, Future $future) {
$tree = $this->getXHPASTTreeForPath($path);
if (!$tree) {
return;
}
$root = $tree->getRootNode();
$method_codes = array(
'lintStrstrUsedForCheck' => self::LINT_SLOWNESS,
'lintStrposUsedForStart' => self::LINT_SLOWNESS,
'lintPHP53Features' => self::LINT_PHP_53_FEATURES,
'lintPHP54Features' => self::LINT_PHP_54_FEATURES,
'lintImplicitFallthrough' => self::LINT_IMPLICIT_FALLTHROUGH,
'lintBraceFormatting' => self::LINT_BRACE_FORMATTING,
'lintTautologicalExpressions' => self::LINT_TAUTOLOGICAL_EXPRESSION,
'lintCommentSpaces' => self::LINT_COMMENT_SPACING,
'lintHashComments' => self::LINT_COMMENT_STYLE,
'lintReusedIterators' => self::LINT_REUSED_ITERATORS,
'lintReusedIteratorReferences' => self::LINT_REUSED_ITERATOR_REFERENCE,
'lintVariableVariables' => self::LINT_VARIABLE_VARIABLE,
'lintUndeclaredVariables' => array(
self::LINT_EXTRACT_USE,
self::LINT_REUSED_AS_ITERATOR,
self::LINT_UNDECLARED_VARIABLE,
),
'lintPHPTagUse' => array(
self::LINT_PHP_SHORT_TAG,
self::LINT_PHP_ECHO_TAG,
self::LINT_PHP_OPEN_TAG,
self::LINT_PHP_CLOSE_TAG,
),
'lintNamingConventions' => self::LINT_NAMING_CONVENTIONS,
'lintSurpriseConstructors' => self::LINT_IMPLICIT_CONSTRUCTOR,
'lintParenthesesShouldHugExpressions' => self::LINT_PARENTHESES_SPACING,
'lintSpaceAfterControlStatementKeywords' =>
self::LINT_CONTROL_STATEMENT_SPACING,
'lintSpaceAroundBinaryOperators' => self::LINT_BINARY_EXPRESSION_SPACING,
'lintDynamicDefines' => self::LINT_DYNAMIC_DEFINE,
'lintUseOfThisInStaticMethods' => self::LINT_STATIC_THIS,
'lintPregQuote' => self::LINT_PREG_QUOTE_MISUSE,
'lintExitExpressions' => self::LINT_EXIT_EXPRESSION,
'lintArrayIndexWhitespace' => self::LINT_ARRAY_INDEX_SPACING,
'lintTODOComments' => self::LINT_TODO_COMMENT,
'lintPrimaryDeclarationFilenameMatch' =>
self::LINT_CLASS_FILENAME_MISMATCH,
'lintPlusOperatorOnStrings' => self::LINT_PLUS_OPERATOR_ON_STRINGS,
'lintDuplicateKeysInArray' => self::LINT_DUPLICATE_KEYS_IN_ARRAY,
'lintRaggedClasstreeEdges' => self::LINT_RAGGED_CLASSTREE_EDGE,
'lintClosingCallParen' => self::LINT_CLOSING_CALL_PAREN,
'lintClosingDeclarationParen' => self::LINT_CLOSING_DECL_PAREN,
'lintKeywordCasing' => self::LINT_KEYWORD_CASING,
);
foreach ($method_codes as $method => $codes) {
foreach ((array)$codes as $code) {
if ($this->isCodeEnabled($code)) {
call_user_func(array($this, $method), $root);
break;
}
}
}
}
public function lintStrstrUsedForCheck($root) {
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildOfType(1, 'n_OPERATOR');
$operator = $operator->getConcreteString();
if ($operator != '===' && $operator != '!==') {
continue;
}
$false = $expression->getChildByIndex(0);
if ($false->getTypeName() == 'n_SYMBOL_NAME' &&
$false->getConcreteString() == 'false') {
$strstr = $expression->getChildByIndex(2);
} else {
$strstr = $false;
$false = $expression->getChildByIndex(2);
if ($false->getTypeName() != 'n_SYMBOL_NAME' ||
$false->getConcreteString() != 'false') {
continue;
}
}
if ($strstr->getTypeName() != 'n_FUNCTION_CALL') {
continue;
}
$name = strtolower($strstr->getChildByIndex(0)->getConcreteString());
if ($name == 'strstr' || $name == 'strchr') {
$this->raiseLintAtNode(
$strstr,
self::LINT_SLOWNESS,
"Use strpos() for checking if the string contains something.");
} else if ($name == 'stristr') {
$this->raiseLintAtNode(
$strstr,
self::LINT_SLOWNESS,
"Use stripos() for checking if the string contains something.");
}
}
}
public function lintStrposUsedForStart($root) {
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildOfType(1, 'n_OPERATOR');
$operator = $operator->getConcreteString();
if ($operator != '===' && $operator != '!==') {
continue;
}
$zero = $expression->getChildByIndex(0);
if ($zero->getTypeName() == 'n_NUMERIC_SCALAR' &&
$zero->getConcreteString() == '0') {
$strpos = $expression->getChildByIndex(2);
} else {
$strpos = $zero;
$zero = $expression->getChildByIndex(2);
if ($zero->getTypeName() != 'n_NUMERIC_SCALAR' ||
$zero->getConcreteString() != '0') {
continue;
}
}
if ($strpos->getTypeName() != 'n_FUNCTION_CALL') {
continue;
}
$name = strtolower($strpos->getChildByIndex(0)->getConcreteString());
if ($name == 'strpos') {
$this->raiseLintAtNode(
$strpos,
self::LINT_SLOWNESS,
"Use strncmp() for checking if the string starts with something.");
} else if ($name == 'stripos') {
$this->raiseLintAtNode(
$strpos,
self::LINT_SLOWNESS,
"Use strncasecmp() for checking if the string starts with ".
"something.");
}
}
}
public function lintPHP53Features($root) {
$functions = $root->selectTokensOfType('T_FUNCTION');
foreach ($functions as $function) {
$next = $function->getNextToken();
while ($next) {
if ($next->isSemantic()) {
break;
}
$next = $next->getNextToken();
}
if ($next) {
if ($next->getTypeName() == '(') {
$this->raiseLintAtToken(
$function,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but anonymous functions were '.
'not introduced until PHP 5.3.');
}
}
}
$namespaces = $root->selectTokensOfType('T_NAMESPACE');
foreach ($namespaces as $namespace) {
$this->raiseLintAtToken(
$namespace,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but namespaces were not introduced '.
'until PHP 5.3.');
}
// NOTE: This is only "use x;", in anonymous functions the node type is
// n_LEXICAL_VARIABLE_LIST even though both tokens are T_USE.
// TODO: We parse n_USE in a slightly crazy way right now; that would be
// a better selector once it's fixed.
$uses = $root->selectDescendantsOfType('n_USE_LIST');
foreach ($uses as $use) {
$this->raiseLintAtNode(
$use,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but namespaces were not introduced '.
'until PHP 5.3.');
}
$statics = $root->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
foreach ($statics as $static) {
$name = $static->getChildByIndex(0);
if ($name->getTypeName() != 'n_CLASS_NAME') {
continue;
}
if ($name->getConcreteString() == 'static') {
$this->raiseLintAtNode(
$name,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but `static::` was not introduced '.
'until PHP 5.3.');
}
}
$ternaries = $root->selectDescendantsOfType('n_TERNARY_EXPRESSION');
foreach ($ternaries as $ternary) {
$yes = $ternary->getChildByIndex(1);
if ($yes->getTypeName() == 'n_EMPTY') {
$this->raiseLintAtNode(
$ternary,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but short ternary was not '.
'introduced until PHP 5.3.');
}
}
$heredocs = $root->selectDescendantsOfType('n_HEREDOC');
foreach ($heredocs as $heredoc) {
if (preg_match('/^<<<[\'"]/', $heredoc->getConcreteString())) {
$this->raiseLintAtNode(
$heredoc,
self::LINT_PHP_53_FEATURES,
'This codebase targets PHP 5.2, but nowdoc was not introduced until '.
'PHP 5.3.');
}
}
$this->lintPHP53Functions($root);
}
private function lintPHP53Functions($root) {
$target = phutil_get_library_root('arcanist').
'/../resources/php_compat_info.json';
$compat_info = json_decode(file_get_contents($target), true);
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($calls as $call) {
$node = $call->getChildByIndex(0);
$name = strtolower($node->getConcreteString());
$version = idx($compat_info['functions'], $name);
- $windows_version = idx($compat_info['functions_windows'], $name);
+ $windows = idx($compat_info['functions_windows'], $name);
if ($version) {
$this->raiseLintAtNode(
$node,
self::LINT_PHP_53_FEATURES,
"This codebase targets PHP 5.2.3, but `{$name}()` was not ".
"introduced until PHP {$version}.");
} else if (array_key_exists($name, $compat_info['params'])) {
$params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
foreach (array_values($params->getChildren()) as $i => $param) {
$version = idx($compat_info['params'][$name], $i);
if ($version) {
$this->raiseLintAtNode(
$param,
self::LINT_PHP_53_FEATURES,
"This codebase targets PHP 5.2.3, but parameter ".($i + 1)." ".
"of `{$name}()` was not introduced until PHP {$version}.");
}
}
- } else if (version_compare($windows_version, '5.3.0') > 0) {
+ } else if ($windows === '' || version_compare($windows, '5.3.0') > 0) {
$this->raiseLintAtNode(
$node,
self::LINT_PHP_53_FEATURES,
"This codebase targets PHP 5.3.0 on Windows, but `{$name}()` is not ".
- "available there.".
- ($windows_version ? " until PHP {$windows_version}" : "").".");
+ "available there".
+ ($windows ? " until PHP {$windows}" : "").".");
}
}
$classes = $root->selectDescendantsOfType('n_CLASS_NAME');
foreach ($classes as $node) {
$name = strtolower($node->getConcreteString());
$version = idx($compat_info['interfaces'], $name);
$version = idx($compat_info['classes'], $name, $version);
if ($version) {
$this->raiseLintAtNode(
$node,
self::LINT_PHP_53_FEATURES,
"This codebase targets PHP 5.2.3, but `{$name}` was not ".
"introduced until PHP {$version}.");
}
}
}
public function lintPHP54Features($root) {
$indexes = $root->selectDescendantsOfType('n_INDEX_ACCESS');
foreach ($indexes as $index) {
$left = $index->getChildByIndex(0);
switch ($left->getTypeName()) {
case 'n_FUNCTION_CALL':
case 'n_METHOD_CALL':
$this->raiseLintAtNode(
$index->getChildByIndex(1),
self::LINT_PHP_54_FEATURES,
"The f()[...] syntax was not introduced until PHP 5.4, but this ".
"codebase targets an earlier version of PHP. You can rewrite ".
"this expression using idx().");
break;
}
}
}
private function lintImplicitFallthrough($root) {
$hook_obj = null;
$working_copy = $this->getEngine()->getWorkingCopy();
if ($working_copy) {
$hook_class = $working_copy->getConfig('lint.xhpast.switchhook');
$hook_class = $this->getConfig('switchhook', $hook_class);
if ($hook_class) {
$hook_obj = newv($hook_class, array());
assert_instances_of(array($hook_obj), 'ArcanistXHPASTLintSwitchHook');
}
}
$switches = $root->selectDescendantsOfType('n_SWITCH');
foreach ($switches as $switch) {
$blocks = array();
$cases = $switch->selectDescendantsOfType('n_CASE');
foreach ($cases as $case) {
$blocks[] = $case;
}
$defaults = $switch->selectDescendantsOfType('n_DEFAULT');
foreach ($defaults as $default) {
$blocks[] = $default;
}
foreach ($blocks as $key => $block) {
// Collect all the tokens in this block which aren't at top level.
// We want to ignore "break", and "continue" in these blocks.
$lower_level = $block->selectDescendantsOfType('n_WHILE');
$lower_level->add($block->selectDescendantsOfType('n_DO_WHILE'));
$lower_level->add($block->selectDescendantsOfType('n_FOR'));
$lower_level->add($block->selectDescendantsOfType('n_FOREACH'));
$lower_level->add($block->selectDescendantsOfType('n_SWITCH'));
$lower_level_tokens = array();
foreach ($lower_level as $lower_level_block) {
$lower_level_tokens += $lower_level_block->getTokens();
}
// Collect all the tokens in this block which aren't in this scope
// (because they're inside class, function or interface declarations).
// We want to ignore all of these tokens.
$decls = $block->selectDescendantsOfType('n_FUNCTION_DECLARATION');
$decls->add($block->selectDescendantsOfType('n_CLASS_DECLARATION'));
// For completeness; these can't actually have anything.
$decls->add($block->selectDescendantsOfType('n_INTERFACE_DECLARATION'));
$different_scope_tokens = array();
foreach ($decls as $decl) {
$different_scope_tokens += $decl->getTokens();
}
$lower_level_tokens += $different_scope_tokens;
// Get all the trailing nonsemantic tokens, since we need to look for
// "fallthrough" comments past the end of the semantic block.
$tokens = $block->getTokens();
$last = end($tokens);
while ($last && $last = $last->getNextToken()) {
if ($last->isSemantic()) {
break;
}
$tokens[$last->getTokenID()] = $last;
}
$blocks[$key] = array(
$tokens,
$lower_level_tokens,
$different_scope_tokens,
);
}
foreach ($blocks as $token_lists) {
list(
$tokens,
$lower_level_tokens,
$different_scope_tokens) = $token_lists;
// Test each block (case or default statement) to see if it's OK. It's
// OK if:
//
// - it is empty; or
// - it ends in break, return, throw, continue or exit at top level; or
// - it has a comment with "fallthrough" in its text.
// Empty blocks are OK, so we start this at `true` and only set it to
// false if we find a statement.
$block_ok = true;
// Keeps track of whether the current statement is one that validates
// the block (break, return, throw, continue) or something else.
$statement_ok = false;
foreach ($tokens as $token_id => $token) {
if (!$token->isSemantic()) {
// Liberally match "fall" in the comment text so that comments like
// "fallthru", "fall through", "fallthrough", etc., are accepted.
if (preg_match('/fall/i', $token->getValue())) {
$block_ok = true;
break;
}
continue;
}
$tok_type = $token->getTypeName();
if ($tok_type == 'T_FUNCTION' ||
$tok_type == 'T_CLASS' ||
$tok_type == 'T_INTERFACE') {
// These aren't statements, but mark the block as nonempty anyway.
$block_ok = false;
continue;
}
if ($tok_type == ';') {
if ($statement_ok) {
$statment_ok = false;
} else {
$block_ok = false;
}
continue;
}
if ($tok_type == 'T_BREAK' ||
$tok_type == 'T_CONTINUE') {
if (empty($lower_level_tokens[$token_id])) {
$statement_ok = true;
$block_ok = true;
}
continue;
}
if ($tok_type == 'T_RETURN' ||
$tok_type == 'T_THROW' ||
$tok_type == 'T_EXIT' ||
($hook_obj && $hook_obj->checkSwitchToken($token))) {
if (empty($different_scope_tokens[$token_id])) {
$statement_ok = true;
$block_ok = true;
}
continue;
}
}
if (!$block_ok) {
$this->raiseLintAtToken(
head($tokens),
self::LINT_IMPLICIT_FALLTHROUGH,
"This 'case' or 'default' has a nonempty block which does not ".
"end with 'break', 'continue', 'return', 'throw' or 'exit'. Did ".
"you forget to add one of those? If you intend to fall through, ".
"add a '// fallthrough' comment to silence this warning.");
}
}
}
}
private function lintBraceFormatting($root) {
foreach ($root->selectDescendantsOfType('n_STATEMENT_LIST') as $list) {
$tokens = $list->getTokens();
if (!$tokens || head($tokens)->getValue() != '{') {
continue;
}
list($before, $after) = $list->getSurroundingNonsemanticTokens();
if (!$before) {
$first = head($tokens);
// Only insert the space if we're after a closing parenthesis. If
// we're in a construct like "else{}", other rules will insert space
// after the 'else' correctly.
$prev = $first->getPrevToken();
if (!$prev || $prev->getValue() != ')') {
continue;
}
$this->raiseLintAtToken(
$first,
self::LINT_BRACE_FORMATTING,
'Put opening braces on the same line as control statements and '.
'declarations, with a single space before them.',
' '.$first->getValue());
} else if (count($before) == 1) {
$before = reset($before);
if ($before->getValue() != ' ') {
$this->raiseLintAtToken(
$before,
self::LINT_BRACE_FORMATTING,
'Put opening braces on the same line as control statements and '.
'declarations, with a single space before them.',
' ');
}
}
}
}
private function lintTautologicalExpressions($root) {
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
static $operators = array(
'-' => true,
'/' => true,
'-=' => true,
'/=' => true,
'<=' => true,
'<' => true,
'==' => true,
'===' => true,
'!=' => true,
'!==' => true,
'>=' => true,
'>' => true,
);
static $logical = array(
'||' => true,
'&&' => true,
);
foreach ($expressions as $expr) {
$operator = $expr->getChildByIndex(1)->getConcreteString();
if (!empty($operators[$operator])) {
$left = $expr->getChildByIndex(0)->getSemanticString();
$right = $expr->getChildByIndex(2)->getSemanticString();
if ($left == $right) {
$this->raiseLintAtNode(
$expr,
self::LINT_TAUTOLOGICAL_EXPRESSION,
'Both sides of this expression are identical, so it always '.
'evaluates to a constant.');
}
}
if (!empty($logical[$operator])) {
$left = $expr->getChildByIndex(0)->getSemanticString();
$right = $expr->getChildByIndex(2)->getSemanticString();
// NOTE: These will be null to indicate "could not evaluate".
$left = $this->evaluateStaticBoolean($left);
$right = $this->evaluateStaticBoolean($right);
if (($operator == '||' && ($left === true || $right === true)) ||
($operator == '&&' && ($left === false || $right === false))) {
$this->raiseLintAtNode(
$expr,
self::LINT_TAUTOLOGICAL_EXPRESSION,
'The logical value of this expression is static. Did you forget '.
'to remove some debugging code?');
}
}
}
}
/**
* Statically evaluate a boolean value from an XHP tree.
*
* TODO: Improve this and move it to XHPAST proper?
*
* @param string The "semantic string" of a single value.
* @return mixed ##true## or ##false## if the value could be evaluated
* statically; ##null## if static evaluation was not possible.
*/
private function evaluateStaticBoolean($string) {
switch (strtolower($string)) {
case '0':
case 'null':
case 'false':
return false;
case '1':
case 'true':
return true;
}
return null;
}
protected function lintCommentSpaces($root) {
foreach ($root->selectTokensOfType('T_COMMENT') as $comment) {
$value = $comment->getValue();
if ($value[0] != '#') {
$match = null;
if (preg_match('@^(/[/*]+)[^/*\s]@', $value, $match)) {
$this->raiseLintAtOffset(
$comment->getOffset(),
self::LINT_COMMENT_SPACING,
'Put space after comment start.',
$match[1],
$match[1].' ');
}
}
}
}
protected function lintHashComments($root) {
foreach ($root->selectTokensOfType('T_COMMENT') as $comment) {
$value = $comment->getValue();
if ($value[0] != '#') {
continue;
}
$this->raiseLintAtOffset(
$comment->getOffset(),
self::LINT_COMMENT_STYLE,
'Use "//" single-line comments, not "#".',
'#',
(preg_match('/^#\S/', $value) ? '// ' : '//'));
}
}
/**
* Find cases where loops get nested inside each other but use the same
* iterator variable. For example:
*
* COUNTEREXAMPLE
* foreach ($list as $thing) {
* foreach ($stuff as $thing) { // <-- Raises an error for reuse of $thing
* // ...
* }
* }
*
*/
private function lintReusedIterators($root) {
$used_vars = array();
$for_loops = $root->selectDescendantsOfType('n_FOR');
foreach ($for_loops as $for_loop) {
$var_map = array();
// Find all the variables that are assigned to in the for() expression.
$for_expr = $for_loop->getChildOfType(0, 'n_FOR_EXPRESSION');
$bin_exprs = $for_expr->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($bin_exprs as $bin_expr) {
if ($bin_expr->getChildByIndex(1)->getConcreteString() == '=') {
$var = $bin_expr->getChildByIndex(0);
$var_map[$var->getConcreteString()] = $var;
}
}
$used_vars[$for_loop->getID()] = $var_map;
}
$foreach_loops = $root->selectDescendantsOfType('n_FOREACH');
foreach ($foreach_loops as $foreach_loop) {
$var_map = array();
$foreach_expr = $foreach_loop->getChildOftype(0, 'n_FOREACH_EXPRESSION');
// We might use one or two vars, i.e. "foreach ($x as $y => $z)" or
// "foreach ($x as $y)".
$possible_used_vars = array(
$foreach_expr->getChildByIndex(1),
$foreach_expr->getChildByIndex(2),
);
foreach ($possible_used_vars as $var) {
if ($var->getTypeName() == 'n_EMPTY') {
continue;
}
$name = $var->getConcreteString();
$name = trim($name, '&'); // Get rid of ref silliness.
$var_map[$name] = $var;
}
$used_vars[$foreach_loop->getID()] = $var_map;
}
$all_loops = $for_loops->add($foreach_loops);
foreach ($all_loops as $loop) {
$child_for_loops = $loop->selectDescendantsOfType('n_FOR');
$child_foreach_loops = $loop->selectDescendantsOfType('n_FOREACH');
$child_loops = $child_for_loops->add($child_foreach_loops);
$outer_vars = $used_vars[$loop->getID()];
foreach ($child_loops as $inner_loop) {
$inner_vars = $used_vars[$inner_loop->getID()];
$shared = array_intersect_key($outer_vars, $inner_vars);
if ($shared) {
$shared_desc = implode(', ', array_keys($shared));
$message = $this->raiseLintAtNode(
$inner_loop->getChildByIndex(0),
self::LINT_REUSED_ITERATORS,
"This loop reuses iterator variables ({$shared_desc}) from an ".
"outer loop. You might be clobbering the outer iterator. Change ".
"the inner loop to use a different iterator name.");
$locations = array();
foreach ($shared as $var) {
$locations[] = $this->getOtherLocation($var->getOffset());
}
$message->setOtherLocations($locations);
}
}
}
}
/**
* Find cases where a foreach loop is being iterated using a variable
* reference and the same variable is used outside of the loop without
* calling unset() or reassigning the variable to another variable
* reference.
*
* COUNTEREXAMPLE
* foreach ($ar as &$a) {
* // ...
* }
* $a = 1; // <-- Raises an error for using $a
*
*/
protected function lintReusedIteratorReferences($root) {
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
$defs = $fdefs->add($mdefs);
foreach ($defs as $def) {
$body = $def->getChildByIndex(5);
if ($body->getTypeName() == 'n_EMPTY') {
// Abstract method declaration.
continue;
}
$exclude = array();
// Exclude uses of variables, unsets, and foreach loops
// within closures - they are checked on their own
$func_defs = $body->selectDescendantsOfType('n_FUNCTION_DECLARATION');
foreach ($func_defs as $func_def) {
$vars = $func_def->selectDescendantsOfType('n_VARIABLE');
foreach ($vars as $var) {
$exclude[$var->getID()] = true;
}
$unset_lists = $func_def->selectDescendantsOfType('n_UNSET_LIST');
foreach ($unset_lists as $unset_list) {
$exclude[$unset_list->getID()] = true;
}
$foreaches = $func_def->selectDescendantsOfType('n_FOREACH');
foreach ($foreaches as $foreach) {
$exclude[$foreach->getID()] = true;
}
}
// Find all variables that are unset within the scope
$unset_vars = array();
$unset_lists = $body->selectDescendantsOfType('n_UNSET_LIST');
foreach ($unset_lists as $unset_list) {
if (isset($exclude[$unset_list->getID()])) {
continue;
}
$unset_list_vars = $unset_list->selectDescendantsOfType('n_VARIABLE');
foreach ($unset_list_vars as $var) {
$concrete = $this->getConcreteVariableString($var);
$unset_vars[$concrete][] = $var->getOffset();
$exclude[$var->getID()] = true;
}
}
// Find all reference variables in foreach expressions
$reference_vars = array();
$foreaches = $body->selectDescendantsOfType('n_FOREACH');
foreach ($foreaches as $foreach) {
if (isset($exclude[$foreach->getID()])) {
continue;
}
$foreach_expr = $foreach->getChildOfType(0, 'n_FOREACH_EXPRESSION');
$var = $foreach_expr->getChildByIndex(2);
if ($var->getTypeName() != 'n_VARIABLE_REFERENCE') {
continue;
}
$reference = $var->getChildByIndex(0);
if ($reference->getTypeName() != 'n_VARIABLE') {
continue;
}
$reference_name = $this->getConcreteVariableString($reference);
$reference_vars[$reference_name][] = $reference->getOffset();
$exclude[$reference->getID()] = true;
// Exclude uses of the reference variable within the foreach loop
$foreach_vars = $foreach->selectDescendantsOfType('n_VARIABLE');
foreach ($foreach_vars as $var) {
$name = $this->getConcreteVariableString($var);
if ($name == $reference_name) {
$exclude[$var->getID()] = true;
}
}
}
// Allow usage if the reference variable is assigned to another
// reference variable
$binary = $body->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($binary as $expr) {
if ($expr->getChildByIndex(1)->getConcreteString() != '=') {
continue;
}
$lval = $expr->getChildByIndex(0);
if ($lval->getTypeName() != 'n_VARIABLE') {
continue;
}
$rval = $expr->getChildByIndex(2);
if ($rval->getTypeName() != 'n_VARIABLE_REFERENCE') {
continue;
}
// Counts as unsetting a variable
$concrete = $this->getConcreteVariableString($lval);
$unset_vars[$concrete][] = $lval->getOffset();
$exclude[$lval->getID()] = true;
}
$all_vars = array();
$all = $body->selectDescendantsOfType('n_VARIABLE');
foreach ($all as $var) {
if (isset($exclude[$var->getID()])) {
continue;
}
$name = $this->getConcreteVariableString($var);
if (!isset($reference_vars[$name])) {
continue;
}
// Find the closest reference offset to this variable
$reference_offset = null;
foreach ($reference_vars[$name] as $offset) {
if ($offset < $var->getOffset()) {
$reference_offset = $offset;
} else {
break;
}
}
if (!$reference_offset) {
continue;
}
// Check if an unset exists between reference and usage of this
// variable
$warn = true;
if (isset($unset_vars[$name])) {
foreach ($unset_vars[$name] as $unset_offset) {
if ($unset_offset > $reference_offset &&
$unset_offset < $var->getOffset()) {
$warn = false;
break;
}
}
}
if ($warn) {
$this->raiseLintAtNode(
$var,
self::LINT_REUSED_ITERATOR_REFERENCE,
'This variable was used already as a by-reference iterator ' .
'variable. Such variables survive outside the foreach loop, ' .
'do not reuse.');
}
}
}
}
protected function lintVariableVariables($root) {
$vvars = $root->selectDescendantsOfType('n_VARIABLE_VARIABLE');
foreach ($vvars as $vvar) {
$this->raiseLintAtNode(
$vvar,
self::LINT_VARIABLE_VARIABLE,
'Rewrite this code to use an array. Variable variables are unclear '.
'and hinder static analysis.');
}
}
protected function lintUndeclaredVariables($root) {
// These things declare variables in a function:
// Explicit parameters
// Assignment
// Assignment via list()
// Static
// Global
// Lexical vars
// Builtins ($this)
// foreach()
// catch
//
// These things make lexical scope unknowable:
// Use of extract()
// Assignment to variable variables ($$x)
// Global with variable variables
//
// These things don't count as "using" a variable:
// isset()
// empty()
// Static class variables
//
// The general approach here is to find each function/method declaration,
// then:
//
// 1. Identify all the variable declarations, and where they first occur
// in the function/method declaration.
// 2. Identify all the uses that don't really count (as above).
// 3. Everything else must be a use of a variable.
// 4. For each variable, check if any uses occur before the declaration
// and warn about them.
//
// We also keep track of where lexical scope becomes unknowable (e.g.,
// because the function calls extract() or uses dynamic variables,
// preventing us from keeping track of which variables are defined) so we
// can stop issuing warnings after that.
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
$defs = $fdefs->add($mdefs);
foreach ($defs as $def) {
// We keep track of the first offset where scope becomes unknowable, and
// silence any warnings after that. Default it to INT_MAX so we can min()
// it later to keep track of the first problem we encounter.
$scope_destroyed_at = PHP_INT_MAX;
$declarations = array(
'$this' => 0,
) + array_fill_keys($this->getSuperGlobalNames(), 0);
$declaration_tokens = array();
$exclude_tokens = array();
$vars = array();
// First up, find all the different kinds of declarations, as explained
// above. Put the tokens into the $vars array.
$param_list = $def->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
$param_vars = $param_list->selectDescendantsOfType('n_VARIABLE');
foreach ($param_vars as $var) {
$vars[] = $var;
}
// This is PHP5.3 closure syntax: function () use ($x) {};
$lexical_vars = $def
->getChildByIndex(4)
->selectDescendantsOfType('n_VARIABLE');
foreach ($lexical_vars as $var) {
$vars[] = $var;
}
$body = $def->getChildByIndex(5);
if ($body->getTypeName() == 'n_EMPTY') {
// Abstract method declaration.
continue;
}
$static_vars = $body
->selectDescendantsOfType('n_STATIC_DECLARATION')
->selectDescendantsOfType('n_VARIABLE');
foreach ($static_vars as $var) {
$vars[] = $var;
}
$global_vars = $body
->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
foreach ($global_vars as $var_list) {
foreach ($var_list->getChildren() as $var) {
if ($var->getTypeName() == 'n_VARIABLE') {
$vars[] = $var;
} else {
// Dynamic global variable, i.e. "global $$x;".
$scope_destroyed_at = min($scope_destroyed_at, $var->getOffset());
// An error is raised elsewhere, no need to raise here.
}
}
}
$catches = $body
->selectDescendantsOfType('n_CATCH')
->selectDescendantsOfType('n_VARIABLE');
foreach ($catches as $var) {
$vars[] = $var;
}
$binary = $body->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($binary as $expr) {
if ($expr->getChildByIndex(1)->getConcreteString() != '=') {
continue;
}
$lval = $expr->getChildByIndex(0);
if ($lval->getTypeName() == 'n_VARIABLE') {
$vars[] = $lval;
} else if ($lval->getTypeName() == 'n_LIST') {
// Recursivey grab everything out of list(), since the grammar
// permits list() to be nested. Also note that list() is ONLY valid
// as an lval assignments, so we could safely lift this out of the
// n_BINARY_EXPRESSION branch.
$assign_vars = $lval->selectDescendantsOfType('n_VARIABLE');
foreach ($assign_vars as $var) {
$vars[] = $var;
}
}
if ($lval->getTypeName() == 'n_VARIABLE_VARIABLE') {
$scope_destroyed_at = min($scope_destroyed_at, $lval->getOffset());
// No need to raise here since we raise an error elsewhere.
}
}
$calls = $body->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($calls as $call) {
$name = strtolower($call->getChildByIndex(0)->getConcreteString());
if ($name == 'empty' || $name == 'isset') {
$params = $call
->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
->selectDescendantsOfType('n_VARIABLE');
foreach ($params as $var) {
$exclude_tokens[$var->getID()] = true;
}
continue;
}
if ($name != 'extract') {
continue;
}
$scope_destroyed_at = min($scope_destroyed_at, $call->getOffset());
$this->raiseLintAtNode(
$call,
self::LINT_EXTRACT_USE,
'Avoid extract(). It is confusing and hinders static analysis.');
}
// Now we have every declaration except foreach(), handled below. Build
// two maps, one which just keeps track of which tokens are part of
// declarations ($declaration_tokens) and one which has the first offset
// where a variable is declared ($declarations).
foreach ($vars as $var) {
$concrete = $this->getConcreteVariableString($var);
$declarations[$concrete] = min(
idx($declarations, $concrete, PHP_INT_MAX),
$var->getOffset());
$declaration_tokens[$var->getID()] = true;
}
// Excluded tokens are ones we don't "count" as being uses, described
// above. Put them into $exclude_tokens.
$class_statics = $body
->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
$class_static_vars = $class_statics
->selectDescendantsOfType('n_VARIABLE');
foreach ($class_static_vars as $var) {
$exclude_tokens[$var->getID()] = true;
}
// Find all the variables in scope, and figure out where they are used.
// We want to find foreach() iterators which are both declared before and
// used after the foreach() loop.
$uses = array();
$all_vars = $body->selectDescendantsOfType('n_VARIABLE');
$all = array();
// NOTE: $all_vars is not a real array so we can't unset() it.
foreach ($all_vars as $var) {
// Be strict since it's easier; we don't let you reuse an iterator you
// declared before a loop after the loop, even if you're just assigning
// to it.
$concrete = $var->getConcreteString();
$uses[$concrete][$var->getID()] = $var->getOffset();
if (isset($declaration_tokens[$var->getID()])) {
// We know this is part of a declaration, so it's fine.
continue;
}
if (isset($exclude_tokens[$var->getID()])) {
// We know this is part of isset() or similar, so it's fine.
continue;
}
$all[$var->getID()] = $var;
}
// Do foreach() last, we want to handle implicit redeclaration of a
// variable already in scope since this probably means we're ovewriting a
// local.
// NOTE: Processing foreach expressions in order allows programs which
// reuse iterator variables in other foreach() loops -- this is fine. We
// have a separate warning to prevent nested loops from reusing the same
// iterators.
$foreaches = $body->selectDescendantsOfType('n_FOREACH');
$all_foreach_vars = array();
foreach ($foreaches as $foreach) {
$foreach_expr = $foreach->getChildOfType(0, 'n_FOREACH_EXPRESSION');
$foreach_vars = array();
// Determine the end of the foreach() loop.
$foreach_tokens = $foreach->getTokens();
$last_token = end($foreach_tokens);
$foreach_end = $last_token->getOffset();
$key_var = $foreach_expr->getChildByIndex(1);
if ($key_var->getTypeName() == 'n_VARIABLE') {
$foreach_vars[] = $key_var;
}
$value_var = $foreach_expr->getChildByIndex(2);
if ($value_var->getTypeName() == 'n_VARIABLE') {
$foreach_vars[] = $value_var;
} else {
// The root-level token may be a reference, as in:
// foreach ($a as $b => &$c) { ... }
// Reach into the n_VARIABLE_REFERENCE node to grab the n_VARIABLE
// node.
$var = $value_var->getChildByIndex(0);
if ($var->getTypeName() == 'n_VARIABLE_VARIABLE') {
$var = $var->getChildByIndex(0);
}
$foreach_vars[] = $var;
}
// Remove all uses of the iterators inside of the foreach() loop from
// the $uses map.
foreach ($foreach_vars as $var) {
$concrete = $this->getConcreteVariableString($var);
$offset = $var->getOffset();
foreach ($uses[$concrete] as $id => $use_offset) {
if (($use_offset >= $offset) && ($use_offset < $foreach_end)) {
unset($uses[$concrete][$id]);
}
}
$all_foreach_vars[] = $var;
}
}
foreach ($all_foreach_vars as $var) {
$concrete = $this->getConcreteVariableString($var);
$offset = $var->getOffset();
// If a variable was declared before a foreach() and is used after
// it, raise a message.
if (isset($declarations[$concrete])) {
if ($declarations[$concrete] < $offset) {
if (!empty($uses[$concrete]) &&
max($uses[$concrete]) > $offset) {
$message = $this->raiseLintAtNode(
$var,
self::LINT_REUSED_AS_ITERATOR,
'This iterator variable is a previously declared local '.
'variable. To avoid overwriting locals, do not reuse them '.
'as iterator variables.');
$message->setOtherLocations(array(
$this->getOtherLocation($declarations[$concrete]),
$this->getOtherLocation(max($uses[$concrete])),
));
}
}
}
// This is a declaration, exclude it from the "declare variables prior
// to use" check below.
unset($all[$var->getID()]);
$vars[] = $var;
}
// Now rebuild declarations to include foreach().
foreach ($vars as $var) {
$concrete = $this->getConcreteVariableString($var);
$declarations[$concrete] = min(
idx($declarations, $concrete, PHP_INT_MAX),
$var->getOffset());
$declaration_tokens[$var->getID()] = true;
}
// Issue a warning for every variable token, unless it appears in a
// declaration, we know about a prior declaration, we have explicitly
// exlcuded it, or scope has been made unknowable before it appears.
$issued_warnings = array();
foreach ($all as $id => $var) {
if ($var->getOffset() >= $scope_destroyed_at) {
// This appears after an extract() or $$var so we have no idea
// whether it's legitimate or not. We raised a harshly-worded warning
// when scope was made unknowable, so just ignore anything we can't
// figure out.
continue;
}
$concrete = $this->getConcreteVariableString($var);
if ($var->getOffset() >= idx($declarations, $concrete, PHP_INT_MAX)) {
// The use appears after the variable is declared, so it's fine.
continue;
}
if (!empty($issued_warnings[$concrete])) {
// We've already issued a warning for this variable so we don't need
// to issue another one.
continue;
}
$this->raiseLintAtNode(
$var,
self::LINT_UNDECLARED_VARIABLE,
'Declare variables prior to use (even if you are passing them '.
'as reference parameters). You may have misspelled this '.
'variable name.');
$issued_warnings[$concrete] = true;
}
}
}
private function getConcreteVariableString($var) {
$concrete = $var->getConcreteString();
// Strip off curly braces as in $obj->{$property}.
$concrete = trim($concrete, '{}');
return $concrete;
}
protected function lintPHPTagUse($root) {
$tokens = $root->getTokens();
foreach ($tokens as $token) {
if ($token->getTypeName() == 'T_OPEN_TAG') {
if (trim($token->getValue()) == '<?') {
$this->raiseLintAtToken(
$token,
self::LINT_PHP_SHORT_TAG,
'Use the full form of the PHP open tag, "<?php".',
"<?php\n");
}
break;
} else if ($token->getTypeName() == 'T_OPEN_TAG_WITH_ECHO') {
$this->raiseLintAtToken(
$token,
self::LINT_PHP_ECHO_TAG,
'Avoid the PHP echo short form, "<?=".');
break;
} else {
if (!preg_match('/^#!/', $token->getValue())) {
$this->raiseLintAtToken(
$token,
self::LINT_PHP_OPEN_TAG,
'PHP files should start with "<?php", which may be preceded by '.
'a "#!" line for scripts.');
}
break;
}
}
foreach ($root->selectTokensOfType('T_CLOSE_TAG') as $token) {
$this->raiseLintAtToken(
$token,
self::LINT_PHP_CLOSE_TAG,
'Do not use the PHP closing tag, "?>".');
}
}
protected function lintNamingConventions($root) {
// We're going to build up a list of <type, name, token, error> tuples
// and then try to instantiate a hook class which has the opportunity to
// override us.
$names = array();
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$name_token = $class->getChildByIndex(1);
$name_string = $name_token->getConcreteString();
$names[] = array(
'class',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string)
? null
: 'Follow naming conventions: classes should be named using '.
'UpperCamelCase.',
);
}
$ifaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION');
foreach ($ifaces as $iface) {
$name_token = $iface->getChildByIndex(1);
$name_string = $name_token->getConcreteString();
$names[] = array(
'interface',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string)
? null
: 'Follow naming conventions: interfaces should be named using '.
'UpperCamelCase.',
);
}
$functions = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
foreach ($functions as $function) {
$name_token = $function->getChildByIndex(2);
if ($name_token->getTypeName() == 'n_EMPTY') {
// Unnamed closure.
continue;
}
$name_string = $name_token->getConcreteString();
$names[] = array(
'function',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string))
? null
: 'Follow naming conventions: functions should be named using '.
'lowercase_with_underscores.',
);
}
$methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$name_token = $method->getChildByIndex(2);
$name_string = $name_token->getConcreteString();
$names[] = array(
'method',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isLowerCamelCase(
ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string))
? null
: 'Follow naming conventions: methods should be named using '.
'lowerCamelCase.',
);
}
$param_tokens = array();
$params = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST');
foreach ($params as $param_list) {
foreach ($param_list->getChildren() as $param) {
$name_token = $param->getChildByIndex(1);
if ($name_token->getTypeName() == 'n_VARIABLE_REFERENCE') {
$name_token = $name_token->getChildOfType(0, 'n_VARIABLE');
}
$param_tokens[$name_token->getID()] = true;
$name_string = $name_token->getConcreteString();
$names[] = array(
'parameter',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string))
? null
: 'Follow naming conventions: parameters should be named using '.
'lowercase_with_underscores.',
);
}
}
$constants = $root->selectDescendantsOfType(
'n_CLASS_CONSTANT_DECLARATION_LIST');
foreach ($constants as $constant_list) {
foreach ($constant_list->getChildren() as $constant) {
$name_token = $constant->getChildByIndex(0);
$name_string = $name_token->getConcreteString();
$names[] = array(
'constant',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isUppercaseWithUnderscores($name_string)
? null
: 'Follow naming conventions: class constants should be named '.
'using UPPERCASE_WITH_UNDERSCORES.',
);
}
}
$member_tokens = array();
$props = $root->selectDescendantsOfType('n_CLASS_MEMBER_DECLARATION_LIST');
foreach ($props as $prop_list) {
foreach ($prop_list->getChildren() as $token_id => $prop) {
if ($prop->getTypeName() == 'n_CLASS_MEMBER_MODIFIER_LIST') {
continue;
}
$name_token = $prop->getChildByIndex(0);
$member_tokens[$name_token->getID()] = true;
$name_string = $name_token->getConcreteString();
$names[] = array(
'member',
$name_string,
$name_token,
ArcanistXHPASTLintNamingHook::isLowerCamelCase(
ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string))
? null
: 'Follow naming conventions: class properties should be named '.
'using lowerCamelCase.',
);
}
}
$superglobal_map = array_fill_keys(
$this->getSuperGlobalNames(),
true);
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
$defs = $fdefs->add($mdefs);
foreach ($defs as $def) {
$globals = $def->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
$globals = $globals->selectDescendantsOfType('n_VARIABLE');
$globals_map = array();
foreach ($globals as $global) {
$global_string = $global->getConcreteString();
$globals_map[$global_string] = true;
$names[] = array(
'global',
$global_string,
$global,
// No advice for globals, but hooks have an option to provide some.
null);
}
// Exclude access of static properties, since lint will be raised at
// their declaration if they're invalid and they may not conform to
// variable rules. This is slightly overbroad (includes the entire
// rhs of a "Class::..." token) to cover cases like "Class:$x[0]". These
// varaibles are simply made exempt from naming conventions.
$exclude_tokens = array();
$statics = $def->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
foreach ($statics as $static) {
$rhs = $static->getChildByIndex(1);
$rhs_vars = $def->selectDescendantsOfType('n_VARIABLE');
foreach ($rhs_vars as $var) {
$exclude_tokens[$var->getID()] = true;
}
}
$vars = $def->selectDescendantsOfType('n_VARIABLE');
foreach ($vars as $token_id => $var) {
if (isset($member_tokens[$token_id])) {
continue;
}
if (isset($param_tokens[$token_id])) {
continue;
}
if (isset($exclude_tokens[$token_id])) {
continue;
}
$var_string = $var->getConcreteString();
// Awkward artifact of "$o->{$x}".
$var_string = trim($var_string, '{}');
if (isset($superglobal_map[$var_string])) {
continue;
}
if (isset($globals_map[$var_string])) {
continue;
}
$names[] = array(
'variable',
$var_string,
$var,
ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
ArcanistXHPASTLintNamingHook::stripPHPVariable($var_string))
? null
: 'Follow naming conventions: variables should be named using '.
'lowercase_with_underscores.',
);
}
}
$engine = $this->getEngine();
$working_copy = $engine->getWorkingCopy();
if ($working_copy) {
// If a naming hook is configured, give it a chance to override the
// default results for all the symbol names.
$hook_class = $working_copy->getConfig('lint.xhpast.naminghook');
if ($hook_class) {
$hook_obj = newv($hook_class, array());
foreach ($names as $k => $name_attrs) {
list($type, $name, $token, $default) = $name_attrs;
$result = $hook_obj->lintSymbolName($type, $name, $default);
$names[$k][3] = $result;
}
}
}
// Raise anything we're left with.
foreach ($names as $k => $name_attrs) {
list($type, $name, $token, $result) = $name_attrs;
if ($result) {
$this->raiseLintAtNode(
$token,
self::LINT_NAMING_CONVENTIONS,
$result);
}
}
}
protected function lintSurpriseConstructors($root) {
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$class_name = $class->getChildByIndex(1)->getConcreteString();
$methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$method_name_token = $method->getChildByIndex(2);
$method_name = $method_name_token->getConcreteString();
if (strtolower($class_name) == strtolower($method_name)) {
$this->raiseLintAtNode(
$method_name_token,
self::LINT_IMPLICIT_CONSTRUCTOR,
'Name constructors __construct() explicitly. This method is a '.
'constructor because it has the same name as the class it is '.
'defined in.');
}
}
}
}
protected function lintParenthesesShouldHugExpressions($root) {
$calls = $root->selectDescendantsOfType('n_CALL_PARAMETER_LIST');
$controls = $root->selectDescendantsOfType('n_CONTROL_CONDITION');
$fors = $root->selectDescendantsOfType('n_FOR_EXPRESSION');
$foreach = $root->selectDescendantsOfType('n_FOREACH_EXPRESSION');
$decl = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST');
$all_paren_groups = $calls
->add($controls)
->add($fors)
->add($foreach)
->add($decl);
foreach ($all_paren_groups as $group) {
$tokens = $group->getTokens();
$token_o = array_shift($tokens);
$token_c = array_pop($tokens);
if ($token_o->getTypeName() != '(') {
throw new Exception('Expected open paren!');
}
if ($token_c->getTypeName() != ')') {
throw new Exception('Expected close paren!');
}
$nonsem_o = $token_o->getNonsemanticTokensAfter();
$nonsem_c = $token_c->getNonsemanticTokensBefore();
if (!$nonsem_o) {
continue;
}
$raise = array();
$string_o = implode('', mpull($nonsem_o, 'getValue'));
if (preg_match('/^[ ]+$/', $string_o)) {
$raise[] = array($nonsem_o, $string_o);
}
if ($nonsem_o !== $nonsem_c) {
$string_c = implode('', mpull($nonsem_c, 'getValue'));
if (preg_match('/^[ ]+$/', $string_c)) {
$raise[] = array($nonsem_c, $string_c);
}
}
foreach ($raise as $warning) {
list($tokens, $string) = $warning;
$this->raiseLintAtOffset(
reset($tokens)->getOffset(),
self::LINT_PARENTHESES_SPACING,
'Parentheses should hug their contents.',
$string,
'');
}
}
}
protected function lintSpaceAfterControlStatementKeywords($root) {
foreach ($root->getTokens() as $id => $token) {
switch ($token->getTypeName()) {
case 'T_IF':
case 'T_ELSE':
case 'T_FOR':
case 'T_FOREACH':
case 'T_WHILE':
case 'T_DO':
case 'T_SWITCH':
$after = $token->getNonsemanticTokensAfter();
if (empty($after)) {
$this->raiseLintAtToken(
$token,
self::LINT_CONTROL_STATEMENT_SPACING,
'Convention: put a space after control statements.',
$token->getValue().' ');
} else if (count($after) == 1) {
$space = head($after);
// If we have an else clause with braces, $space may not be
// a single white space. e.g.,
//
// if ($x)
// echo 'foo'
// else // <- $space is not " " but "\n ".
// echo 'bar'
//
// We just require it starts with either a whitespace or a newline.
if ($token->getTypeName() == 'T_ELSE' ||
$token->getTypeName() == 'T_DO') {
break;
}
if ($space->isAnyWhitespace() && $space->getValue() != ' ') {
$this->raiseLintAtToken(
$space,
self::LINT_CONTROL_STATEMENT_SPACING,
'Convention: put a single space after control statements.',
' ');
}
}
break;
}
}
}
protected function lintSpaceAroundBinaryOperators($root) {
// NOTE: '.' is parsed as n_CONCATENATION_LIST, not n_BINARY_EXPRESSION,
// so we don't select it here.
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildByIndex(1);
$operator_value = $operator->getConcreteString();
list($before, $after) = $operator->getSurroundingNonsemanticTokens();
$replace = null;
if (empty($before) && empty($after)) {
$replace = " {$operator_value} ";
} else if (empty($before)) {
$replace = " {$operator_value}";
} else if (empty($after)) {
$replace = "{$operator_value} ";
}
if ($replace !== null) {
$this->raiseLintAtNode(
$operator,
self::LINT_BINARY_EXPRESSION_SPACING,
'Convention: logical and arithmetic operators should be '.
'surrounded by whitespace.',
$replace);
}
}
$tokens = $root->selectTokensOfType(',');
foreach ($tokens as $token) {
$next = $token->getNextToken();
switch ($next->getTypeName()) {
case ')':
case 'T_WHITESPACE':
break;
break;
default:
$this->raiseLintAtToken(
$token,
self::LINT_BINARY_EXPRESSION_SPACING,
'Convention: comma should be followed by space.',
', ');
break;
}
}
// TODO: Spacing around ".".
// TODO: Spacing around default parameter assignment in function/method
// declarations (which is not n_BINARY_EXPRESSION).
}
protected function lintDynamicDefines($root) {
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($calls as $call) {
$name = $call->getChildByIndex(0)->getConcreteString();
if (strtolower($name) == 'define') {
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
$defined = $parameter_list->getChildByIndex(0);
if (!$defined->isStaticScalar()) {
$this->raiseLintAtNode(
$defined,
self::LINT_DYNAMIC_DEFINE,
'First argument to define() must be a string literal.');
}
}
}
}
protected function lintUseOfThisInStaticMethods($root) {
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$attributes = $method
->getChildByIndex(0, 'n_METHOD_MODIFIER_LIST')
->selectDescendantsOfType('n_STRING');
$method_is_static = false;
$method_is_abstract = false;
foreach ($attributes as $attribute) {
if (strtolower($attribute->getConcreteString()) == 'static') {
$method_is_static = true;
}
if (strtolower($attribute->getConcreteString()) == 'abstract') {
$method_is_abstract = true;
}
}
if ($method_is_abstract) {
continue;
}
if (!$method_is_static) {
continue;
}
$body = $method->getChildOfType(5, 'n_STATEMENT_LIST');
$variables = $body->selectDescendantsOfType('n_VARIABLE');
foreach ($variables as $variable) {
if ($method_is_static &&
strtolower($variable->getConcreteString()) == '$this') {
$this->raiseLintAtNode(
$variable,
self::LINT_STATIC_THIS,
'You can not reference "$this" inside a static method.');
}
}
}
}
}
/**
* preg_quote() takes two arguments, but the second one is optional because
* it is possible to use (), [] or {} as regular expression delimiters. If
* you don't pass a second argument, you're probably going to get something
* wrong.
*/
protected function lintPregQuote($root) {
$function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($function_calls as $call) {
$name = $call->getChildByIndex(0)->getConcreteString();
if (strtolower($name) === 'preg_quote') {
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
if (count($parameter_list->getChildren()) !== 2) {
$this->raiseLintAtNode(
$call,
self::LINT_PREG_QUOTE_MISUSE,
'If you use pattern delimiters that require escaping (such as //, '.
'but not ()) then you should pass two arguments to preg_quote(), '.
'so that preg_quote() knows which delimiter to escape.');
}
}
}
}
/**
* Exit is parsed as an expression, but using it as such is almost always
* wrong. That is, this is valid:
*
* strtoupper(33 * exit - 6);
*
* When exit is used as an expression, it causes the program to terminate with
* exit code 0. This is likely not what is intended; these statements have
* different effects:
*
* exit(-1);
* exit -1;
*
* The former exits with a failure code, the latter with a success code!
*/
protected function lintExitExpressions($root) {
$unaries = $root->selectDescendantsOfType('n_UNARY_PREFIX_EXPRESSION');
foreach ($unaries as $unary) {
$operator = $unary->getChildByIndex(0)->getConcreteString();
if (strtolower($operator) == 'exit') {
if ($unary->getParentNode()->getTypeName() != 'n_STATEMENT') {
$this->raiseLintAtNode(
$unary,
self::LINT_EXIT_EXPRESSION,
"Use exit as a statement, not an expression.");
}
}
}
}
private function lintArrayIndexWhitespace($root) {
$indexes = $root->selectDescendantsOfType('n_INDEX_ACCESS');
foreach ($indexes as $index) {
$tokens = $index->getChildByIndex(0)->getTokens();
$last = array_pop($tokens);
$trailing = $last->getNonsemanticTokensAfter();
$trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^ +$/', $trailing_text)) {
$this->raiseLintAtOffset(
$last->getOffset() + strlen($last->getValue()),
self::LINT_ARRAY_INDEX_SPACING,
'Convention: no spaces before index access.',
$trailing_text,
'');
}
}
}
protected function lintTODOComments($root) {
$comments = $root->selectTokensOfType('T_COMMENT') +
$root->selectTokensOfType('T_DOC_COMMENT');
foreach ($comments as $token) {
$value = $token->getValue();
$matches = null;
$preg = preg_match_all(
'/TODO/',
$value,
$matches,
PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $match) {
list($string, $offset) = $match;
$this->raiseLintAtOffset(
$token->getOffset() + $offset,
self::LINT_TODO_COMMENT,
'This comment has a TODO.',
$string);
}
}
}
/**
* Lint that if the file declares exactly one interface or class,
* the name of the file matches the name of the class,
* unless the classname is funky like an XHP element.
*/
private function lintPrimaryDeclarationFilenameMatch($root) {
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
$interfaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION');
if (count($classes) + count($interfaces) != 1) {
return;
}
$declarations = count($classes) ? $classes : $interfaces;
$declarations->rewind();
$declaration = $declarations->current();
$decl_name = $declaration->getChildByIndex(1);
$decl_string = $decl_name->getConcreteString();
// Exclude strangely named classes, e.g. XHP tags.
if (!preg_match('/^\w+$/', $decl_string)) {
return;
}
$rename = $decl_string.'.php';
$path = $this->getActivePath();
$filename = basename($path);
if ($rename == $filename) {
return;
}
$this->raiseLintAtNode(
$decl_name,
self::LINT_CLASS_FILENAME_MISMATCH,
"The name of this file differs from the name of the class or interface ".
"it declares. Rename the file to '{$rename}'.");
}
private function lintPlusOperatorOnStrings($root) {
$binops = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($binops as $binop) {
$op = $binop->getChildByIndex(1);
if ($op->getConcreteString() != '+') {
continue;
}
$left = $binop->getChildByIndex(0);
$right = $binop->getChildByIndex(2);
if (($left->getTypeName() == 'n_STRING_SCALAR') ||
($right->getTypeName() == 'n_STRING_SCALAR')) {
$this->raiseLintAtNode(
$binop,
self::LINT_PLUS_OPERATOR_ON_STRINGS,
"In PHP, '.' is the string concatenation operator, not '+'. This ".
"expression uses '+' with a string literal as an operand.");
}
}
}
/**
* Finds duplicate keys in array initializers, as in
* array(1 => 'anything', 1 => 'foo'). Since the first entry is ignored,
* this is almost certainly an error.
*/
private function lintDuplicateKeysInArray($root) {
$array_literals = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
foreach ($array_literals as $array_literal) {
$nodes_by_key = array();
$keys_warn = array();
$list_node = $array_literal->getChildByIndex(0);
foreach ($list_node->getChildren() as $array_entry) {
$key_node = $array_entry->getChildByIndex(0);
switch ($key_node->getTypeName()) {
case 'n_STRING_SCALAR':
case 'n_NUMERIC_SCALAR':
// Scalars: array(1 => 'v1', '1' => 'v2');
$key = 'scalar:'.(string)$key_node->evalStatic();
break;
case 'n_SYMBOL_NAME':
case 'n_VARIABLE':
case 'n_CLASS_STATIC_ACCESS':
// Constants: array(CONST => 'v1', CONST => 'v2');
// Variables: array($a => 'v1', $a => 'v2');
// Class constants and vars: array(C::A => 'v1', C::A => 'v2');
$key = $key_node->getTypeName().':'.$key_node->getConcreteString();
break;
default:
$key = null;
break;
}
if ($key !== null) {
if (isset($nodes_by_key[$key])) {
$keys_warn[$key] = true;
}
$nodes_by_key[$key][] = $key_node;
}
}
foreach ($keys_warn as $key => $_) {
$node = array_pop($nodes_by_key[$key]);
$message = $this->raiseLintAtNode(
$node,
self::LINT_DUPLICATE_KEYS_IN_ARRAY,
"Duplicate key in array initializer. PHP will ignore all ".
"but the last entry.");
$locations = array();
foreach ($nodes_by_key[$key] as $node) {
$locations[] = $this->getOtherLocation($node->getOffset());
}
$message->setOtherLocations($locations);
}
}
}
private function lintRaggedClasstreeEdges($root) {
$parser = new PhutilDocblockParser();
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$is_final = false;
$is_abstract = false;
$is_concrete_extensible = false;
$attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES');
foreach ($attributes->getChildren() as $child) {
if ($child->getConcreteString() == 'final') {
$is_final = true;
}
if ($child->getConcreteString() == 'abstract') {
$is_abstract = true;
}
}
$docblock = $class->getDocblockToken();
if ($docblock) {
list($text, $specials) = $parser->parse($docblock->getValue());
$is_concrete_extensible = idx($specials, 'concrete-extensible');
}
if (!$is_final && !$is_abstract && !$is_concrete_extensible) {
$this->raiseLintAtNode(
$class->getChildOfType(1, 'n_CLASS_NAME'),
self::LINT_RAGGED_CLASSTREE_EDGE,
"This class is neither 'final' nor 'abstract', and does not have ".
"a docblock marking it '@concrete-extensible'.");
}
}
}
private function lintClosingCallParen($root) {
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
$calls = $calls->add($root->selectDescendantsOfType('n_METHOD_CALL'));
foreach ($calls as $call) {
// If the last parameter of a call is a HEREDOC, don't apply this rule.
$params = $call
->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
->getChildren();
if ($params) {
$last_param = last($params);
if ($last_param->getTypeName() == 'n_HEREDOC') {
continue;
}
}
$tokens = $call->getTokens();
$last = array_pop($tokens);
$trailing = $last->getNonsemanticTokensBefore();
$trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^\s+$/', $trailing_text)) {
$this->raiseLintAtOffset(
$last->getOffset() - strlen($trailing_text),
self::LINT_CLOSING_CALL_PAREN,
'Convention: no spaces before closing parenthesis in calls.',
$trailing_text,
'');
}
}
}
private function lintClosingDeclarationParen($root) {
$decs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
$decs = $decs->add($root->selectDescendantsOfType('n_METHOD_DECLARATION'));
foreach ($decs as $dec) {
$params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
$tokens = $params->getTokens();
$last = array_pop($tokens);
$trailing = $last->getNonsemanticTokensBefore();
$trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^\s+$/', $trailing_text)) {
$this->raiseLintAtOffset(
$last->getOffset() - strlen($trailing_text),
self::LINT_CLOSING_DECL_PAREN,
'Convention: no spaces before closing parenthesis in function and '.
'method declarations.',
$trailing_text,
'');
}
}
}
private function lintKeywordCasing($root) {
$keywords = array();
$symbols = $root->selectDescendantsOfType('n_SYMBOL_NAME');
foreach ($symbols as $symbol) {
$keywords[] = head($symbol->getTokens());
}
$arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
foreach ($arrays as $array) {
$keywords[] = head($array->getTokens());
}
$typehints = $root->selectDescendantsOfType('n_TYPE_NAME');
foreach ($typehints as $typehint) {
$keywords[] = head($typehint->getTokens());
}
// NOTE: Although PHP generally allows arbitrary casing for all language
// keywords, it's exceedingly rare for anyone to type, e.g., "CLASS" or
// "cLaSs" in the wild. This list just attempts to cover unconventional
// spellings which see some level of use, not all keywords exhaustively.
// There is no token or node type which spans all keywords, so this is
// significantly simpler.
static $keyword_map = array(
'true' => 'true',
'false' => 'false',
'null' => 'null',
'array' => 'array',
);
foreach ($keywords as $keyword) {
$value = $keyword->getValue();
$value_key = strtolower($value);
if (!isset($keyword_map[$value_key])) {
continue;
}
$expected_spelling = $keyword_map[$value_key];
if ($value !== $expected_spelling) {
$this->raiseLintAtToken(
$keyword,
self::LINT_KEYWORD_CASING,
"Convention: spell keyword '{$value}' as '{$expected_spelling}'.",
$expected_spelling);
}
}
}
public function getSuperGlobalNames() {
return array(
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_POST',
'$_FILES',
'$_COOKIE',
'$_SESSION',
'$_REQUEST',
'$_ENV',
);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 22, 07:27 (40 m, 21 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547624
Default Alt Text
(105 KB)

Event Timeline